gpt4 book ai didi

wcf - 仅使用手动代码调用 WCF 服务(无配置或自动生成代码)

转载 作者:行者123 更新时间:2023-12-05 00:19:31 28 4
gpt4 key购买 nike

我大致遵循 WCF The Right Way ... The Manual Way 中的方法设置我的 WCF 服务。

我有一个手动生成的代理类,如下所示:

// Setup a client so we can call our web services.
public class EmployeeClient :IEmployeeService
{
private readonly IEmployeeService EmployeeChannel;

public EmployeeClient(Binding binding, string address)
{
var endpointAddress = new EndpointAddress(address);

EmployeeChannel = new ChannelFactory<IEmployeeService>
(binding, endpointAddress).CreateChannel();
}

public EmployeeResponse SaveOrUpdateEmployee(EmployeeContract employee)
{
return EmployeeChannel.SaveOrUpdateEmployee(employee);
}
}

然后我想调用其中一些服务。但我不想使用任何配置文件(我正在设置一些集成测试,并且我不需要更多的依赖项。)

我目前正在尝试这样称呼他们:

serviceHost = SelfServiceHost.StartupService();

employeeClient = new EmployeeClient(new BasicHttpBinding(),
SelfServiceHost.StartUpUrl);

EmployeeResponse employeeResponse = employeeClient.SaveOrUpdateEmployee(emp);

当我这样做时,我得到了这个异常:

System.ServiceModel.ProtocolException: Content Type text/xml; charset=utf-8 was not supported by service http://localhost:8090/EmployeeService. The client and service bindings may be mismatched. ---> System.Net.WebException: The remote server returned an error: (415) Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'..

我需要做什么才能调用仅使用代码的服务?

最佳答案

根据您的描述,绑定(bind)未以兼容的方式配置。

我怀疑 WCF 主机具有 wsHttpBinding 并且您的客户端具有 BasicHttpBinding 或类似...

参见http://social.msdn.microsoft.com/forums/en-US/wcf/thread/f29cd9c8-3c89-43d2-92ae-d2a270ab86b9/

关于wcf - 仅使用手动代码调用 WCF 服务(无配置或自动生成代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7096472/

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