自托管 WCF 服务无法通过 WCFTestClient 进行测试

2023-12-27

我正在尝试使用 WCFTestClient 测试我的自托管 wcf 服务。我收到这样的错误:

错误:无法从中获取元数据http://localhost:2303/MyService http://localhost:2303/MyService如果这是您有权访问的 Windows (R) Communication Foundation 服务,请检查您是否已在指定地址启用元数据发布。如需启用元数据发布的帮助,请参阅 MSDN 文档:http://go.microsoft.com/fwlink/?LinkId=65455.WS-元数据 http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata交换错误 URI:http://localhost:2303/MyService http://localhost:2303/MyService元数据包含无法解析的引用:“http://localhost:2303/MyService”。内容类型 application/soap+xml;服务不支持 charset=utf-8http://localhost:2303/MyService http://localhost:2303/MyService。客户端和服务绑定可能不匹配。远程服务器返回错误:(415) 无法处理消息,因为内容类型为“application/soap+xml;” charset=utf-8' 不是预期的类型 'text/xml; charset=utf-8'..HTTP GET 错误 URI:http://localhost:2303/MyService http://localhost:2303/MyService下载“http://localhost:2303/MyService”时出错。请求失败,HTTP 状态为 400:错误请求。

我的项目结构如下

  1. 充当主机的控制台应用程序
  2. 服务合同
  3. 服务实施

这是我的服务实现和合同类,它们位于两个单独的项目中。

namespace MyService
{
    public class MyService : IMyService
    {
        public string GetGreeting(string name)
        {
            return "Hello " + name;
        }

        public string GetYelling(string name)
        {
            return "What the hell " + name + "!!";
        }
    }
}

namespace MyService
{
    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        string GetGreeting(string name);

        [OperationContract]
        string GetYelling(string name);
    }
}

这是控制台应用程序

namespace MyWCFHost
{
    class Program
    {
        static void Main(string[] args)
        {

            ServiceHost serviceHost = new ServiceHost(typeof(MyService.MyService), new Uri("http://localhost:2303"));
            serviceHost.Open();

            Console.WriteLine("MyService is running...");
            Console.ReadKey();
            serviceHost.Close();
        }
    }
}

这是配置文件

<configuration>

  <system.serviceModel>
    <services>
      <service name ="MyService.MyService" behaviorConfiguration="MyService.MyServiceBehavior">
        <endpoint address="http://localhost:2303/MyService" binding="basicHttpBinding" contract="MyService.IMyService"/>
        <endpoint address="mex" binding="mexHttpBinding" name="mexpoint" contract="IMetadataExchange" />
      </service>

    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="MyService.MyServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

  </system.serviceModel>


</configuration>

我究竟做错了什么?

谢谢你的时间...

Edit

当我尝试通过 winforms 客户端运行该服务时,该服务可以工作,因此我知道该服务正在工作。问题是我如何使用 WcfTestClient 为测试做好准备。


我怀疑您的 MEX 端点有问题。您当前仅指定相对地址(“mex”) - 但您的服务中没有定义 HTTP 的基地址......

我建议:

  • 要么定义一个基地址,然后仅使用该地址“之上”的相对地址 - 对于您的常规端点和 MEX 端点

OR:

  • 将您的地址定义为完整的地址 - 不仅适用于您的常规端点,在这种情况下也适用于 MEX 端点。

因此,将您的配置更改为:

<service name ="MyService.MyService" behaviorConfiguration="MyService.MyServiceBehavior">
    <endpoint 
        address="http://localhost:2303/MyService" 
        binding="basicHttpBinding" 
        contract="MyService.IMyService"/>
    <endpoint name="mexpoint" 
        address="http://localhost:2303/MyService/mex" 
        binding="mexHttpBinding" 
        contract="IMetadataExchange" />
  </service>

然后我希望您应该能够获取您的元数据,从而连接到您的服务!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

自托管 WCF 服务无法通过 WCFTestClient 进行测试 的相关文章

随机推荐