2

我松散地遵循WCF The Right 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; 字符集=utf-8'..

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

4

1 回答 1

6

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

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

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

于 2011-08-17T16:51:19.117 回答