简体   繁体   English

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

[英]Selfhosted WCF service cant be tested via WCFTestClient

I am trying to test my self hosted wcf service using WCFTestClient. 我正在尝试使用WCFTestClient测试我自己托管的wcf服务。 I get an error like so: 我得到一个错误:

Error: Cannot obtain Metadata from http://localhost:2303/MyService If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. 错误:无法从http:// localhost:2303 / MyService获取元数据如果这是您有权访问的Windows(R)Communication Foundation服务,请检查您是否已在指定地址启用元数据发布。 For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:2303/MyService Metadata contains a reference that cannot be resolved: 'http://localhost:2303/MyService'. 有关启用元数据发布的帮助,请参阅http://go.microsoft.com/fwlink/?LinkId=65455上的MSDN文档.WS- Metadata Exchange错误URI: http:// localhost:2303 / MyService元数据包含引用无法解决:'http:// localhost:2303 / MyService'。 Content Type application/soap+xml; 内容类型application / soap + xml; charset=utf-8 was not supported by service http://localhost:2303/MyService . 服务http:// localhost:2303 / MyService不支持charset = utf-8。 The client and service bindings may be mismatched. 客户端和服务绑定可能不匹配。 The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; 远程服务器返回错误:(415)无法处理消息,因为内容类型为'application / soap + xml; charset=utf-8' was not the expected type 'text/xml; charset = utf-8'不是预期的类型'text / xml; charset=utf-8'..HTTP GET Error URI: http://localhost:2303/MyService There was an error downloading 'http://localhost:2303/MyService'. charset = utf-8'.. HTTP GET错误URI: http:// localhost:2303 / MyService下载'http:// localhost:2303 / MyService'时出错。 The request failed with HTTP status 400: Bad Request. 请求失败,HTTP状态为400:错误请求。

My project structure is as follows 我的项目结构如下

  1. Console Application that acts as host 作为主机的控制台应用程序
  2. Service Contract 服务合约
  3. Service implementation 服务实施

Here are my service implementation and Contract classes, which are in two separate projects. 这是我的服务实现和合同类,它们位于两个独立的项目中。

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);
    }
}

This is the console app 这是控制台应用程序

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();
        }
    }
}

This is the config file 这是配置文件

<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>

What am I doing wrong? 我究竟做错了什么?

Thanks for your time... 谢谢你的时间...

Edit 编辑

The service works when I try to run it via a winforms client so I know the service is working. 当我尝试通过winforms客户端运行它时服务工作,所以我知道服务正在运行。 Question is how do I get it ready for testing as well, using WcfTestClient. 问题是如何使用WcfTestClient为测试做好准备。

I suspect you have a problem with your MEX endpoint. 我怀疑你的MEX端点有问题。 You're currently only specifying a relative address ("mex") - but there's no base address for HTTP defined in your service ...... 您目前只指定相对地址(“mex”) - 但是您的服务中没有定义HTTP的基地址......

I would suggest to: 我建议:

  • either define a base address and then use only relative addresses "on top" of that - for your regular and your MEX endpoint 要么定义一个基地址,然后只使用相对地址“on the top” - 对于常规和MEX端点

OR: 要么:

  • define your addresses as full, complete addresses - not just for your regular endpoints, but in that case for the MEX endpoint as well. 将您的地址定义为完整,完整的地址 - 不仅适用于您的常规端点,还适用于MEX端点。

So change your config to be something like: 因此,将配置更改为:

<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>

and then I hope you should be able to get at your metadata and thus connect to your service! 然后我希望你能够获得你的元数据,从而连接到你的服务!

Are you running Windows 7? 你在运行Windows 7吗?

Try running: 试试跑步:

netsh http add urlacl url=http://+:2303/MyService user=DOMAIN\user

More info and here too 更多信息和这里

Try to run your visual studio as administrator. 尝试以管理员身份运行Visual Studio。 Then you do not need to manually run the commands like (url=http://+:2303/MyService user=PCNAME\\mylogin ) 然后你不需要手动运行像(url = http:// +:2303 / MyService user = PCNAME \\ mylogin)这样的命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM