微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

测试 WCF - 没有端点监听

如何解决测试 WCF - 没有端点监听

我正在尝试对 WCF 应用程序进行单元测试。 这是我尝试过的:

public class Service1 : IService1
{
    public string GetData(int value)
    {
        string val = ConfigurationManager.AppSettings["mykey"].ToString();
        return string.Format("You entered: {0}. mykey={1}",value,val);
    }
}

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {

        ServiceHost serviceHost = null;
        try
        {
            string url = "http://localhost:56666/Serivce1";
            var baseAddress = new Uri(url);
            serviceHost = new ServiceHost(typeof(Service1),baseAddress);

            Binding binding = new WSHttpBinding(SecurityMode.None);
            serviceHost.AddServiceEndpoint(typeof(IService1),binding,"Service1");

            var smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            serviceHost.Description.Behaviors.Add(smb);

            serviceHost.open(); // if this fails,you have to run Visual Studio as admin

            BasicHttpBinding myBinding = new BasicHttpBinding();
            ChannelFactory<IService1> myChannelFactory = new ChannelFactory<IService1>(myBinding);
            EndpointAddress myEndpoint = new EndpointAddress(url); 
            IService1 wcfClient = myChannelFactory.CreateChannel(myEndpoint);
            string s = wcfClient.GetData(39);

            serviceHost.Close();
        }
        finally
        {
            if (serviceHost != null)
            {
                ((Idisposable)serviceHost).dispose();
            }
        }
    }
}

}

当我调用 wcfClient.GetData(39); 我收到此错误: System.ServiceModel.EndpointNotFoundException:在 http://localhost:56666/Serivce1 上没有可以接受消息的端点侦听。这通常是由不正确的地址或 SOAP 操作引起的。有关更多详细信息,请参阅 InnerException(如果存在)。 ---> System.Net.WebException:远程服务器返回错误:(404) 未找到。

知道为什么会出现此错误以及如何使其正常工作吗?

解决方法

我让它工作了,但使绑定保持一致并修复了与 WCF 配置相关的问题,但没有很好的解决方法:

public class Service1 : IService1
{
    public string GetData(int value)
    {
        string val = "Nothing";
        if(ConfigurationManager.AppSettings != null && ConfigurationManager.AppSettings["mykey"] != null) // when normally run
        {
            val = ConfigurationManager.AppSettings["mykey"].ToString();
        }
        else // when run via unittest:
        {
            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = "web.config";
            Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap,ConfigurationUserLevel.None);
            ConfigurationManager.RefreshSection("appSettings"); // this does not work,so I I wrote the loop:
            foreach (KeyValueConfigurationElement kv in configuration.AppSettings.Settings)
            {
                ConfigurationManager.AppSettings[kv.Key] = kv.Value;
            }

            if (ConfigurationManager.AppSettings["mykey"] != null)
            {
                val = ConfigurationManager.AppSettings["mykey"].ToString();
            }
        }

        return string.Format("You entered: {0}. mykey={1}",value,val);
    }

}


[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {

        ServiceHost serviceHost = null;
        try
        {
            string url = "http://localhost:56669";
            var baseAddress = new Uri(url);
            serviceHost = new ServiceHost(typeof(Service1),baseAddress);

            BasicHttpBinding binding = new BasicHttpBinding();
            serviceHost.AddServiceEndpoint(typeof(IService1),binding,"");

            var smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            serviceHost.Description.Behaviors.Add(smb);

            // enabling server side exception details to help debug:
            var behavior = serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>();
            behavior.IncludeExceptionDetailInFaults = true;

            serviceHost.Open(); // if this fails,you have to run Visual Studio as admin

            ChannelFactory<IService1> myChannelFactory = new ChannelFactory<IService1>(binding);
            EndpointAddress myEndpoint = new EndpointAddress(url); 
            IService1 wcfClient = myChannelFactory.CreateChannel(myEndpoint);
            string result = wcfClient.GetData(39);
            Assert.AreEqual(string.Format("You entered: {0}. mykey=myval",39),result);
            serviceHost.Close();
        }
        finally
        {
            if (serviceHost != null)
            {
                ((IDisposable)serviceHost).Dispose();
            }
        }
    }
}

欢迎任何改进配置加载的建议。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。