测试 WCF - 没有端点监听

Posted

技术标签:

【中文标题】测试 WCF - 没有端点监听【英文标题】:testing WCF - There was no endpoint listening 【发布时间】:2021-09-14 22:46:28 【问题描述】:

我正在尝试对 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)未找到。

任何想法为什么我会收到此错误以及如何使其工作?

【问题讨论】:

可以参考:***.com/questions/24437914/… 谢谢。我可以看到我正在使用的端口已被使用,但即使从另一个程序我也无法与之交谈。请注意,我必须以管理员身份打开 VisualStudio。 【参考方案1】:

我得到了它的工作,但使绑定保持一致并解决了与 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();
            
        
    

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

【讨论】:

以上是关于测试 WCF - 没有端点监听的主要内容,如果未能解决你的问题,请参考以下文章

没有端点监听

net.pipe 没有端点监听

c# WCF 可以像socket那样监听吗?

Windows Phone 7 应用程序 + WCF + SSL + 用户名认证

WCF - 是不是可以使用 HttpListener 或套接字来监听 WCF ChannelFactory

WCF终结点地址和监听地址没这么简单