【问题标题】:Call a WCF Service using just manual code (no config or autogen code)仅使用手动代码(无配置或自动生成代码)调用 WCF 服务
【发布时间】:2011-08-17 16:44:07
【问题描述】:

我大致遵循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:内容类型文本/xml;服务 http://localhost:8090/EmployeeService 不支持 charset=utf-8。客户端和服务绑定可能不匹配。 ---> System.Net.WebException: The remote server returned an error: (415) Cannot processing the message because the content type 'text/xml; charset=utf-8' 不是预期的类型 'application/soap+xml; charset=utf-8'..

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

【问题讨论】:

  • 为了将来参考和在此评论发布时,问题中发布的教程链接不再加载教程。我无法在其他地方找到该教程,但能够在此处找到本教程的快速参考,其中包含示例项目代码:codeproject.com/Articles/114139/…

标签: wcf endpoint


【解决方案1】:

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

我怀疑 WCF 主机有 wsHttpBinding,而您的客户端有 BasicHttpBinding 或类似...

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

【讨论】:

    猜你喜欢
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    相关资源
    最近更新 更多