gpt4 book ai didi

c# - 无法通过 WCFTestClient 测试自托管 WCF 服务

转载 作者:行者123 更新时间:2023-11-30 15:42:51 29 4
gpt4 key购买 nike

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

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. 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'. Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:2303/MyService. 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; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'..HTTP GET Error URI: http://localhost:2303/MyService There was an error downloading 'http://localhost:2303/MyService'. The request failed with HTTP status 400: Bad Request.

我的项目结构如下

  1. 充当主机的控制台应用程序
  2. 服务契约(Contract)
  3. 服务实现

这是我的服务实现和契约(Contract)类,它们在两个不同的项目中。

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>

我做错了什么?

感谢您的宝贵时间...

编辑

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

最佳答案

我怀疑您的 MEX 端点有问题。您目前只指定了一个相对地址(“mex”)——但您的服务中没有定义 HTTP 的基地址……

我建议:

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

或者:

  • 将您的地址定义为完整、完整的地址 - 不仅适用于您的常规端点,在这种情况下也适用于 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>

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

关于c# - 无法通过 WCFTestClient 测试自托管 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7192877/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com