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

Vac*_*ano 2 wcf endpoint

我正在松散地遵循WCF中的方法正确的方法...手动方式来设置我的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);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我想打电话给其中的一些服务.但我不想使用任何配置文件(我正在设置一些集成测试,我不希望有更多的依赖项.)

我目前正试图像这样打电话给他们:

serviceHost = SelfServiceHost.StartupService();

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

EmployeeResponse employeeResponse = employeeClient.SaveOrUpdateEmployee(emp);
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我得到这个例外:

System.ServiceModel.ProtocolException:Content Type text/xml; 服务http:// localhost:8090/EmployeeService不支持charset = utf-8 .客户端和服务绑定可能不匹配.---> System.Net.WebException:远程服务器返回错误:(415)无法处理消息,因为内容类型为'text/xml; charset = utf-8'不是预期的类型'application/soap + xml; 字符集= UTF-8' ..

如果仅使用代码调用我的服务,我需要做什么?

Yah*_*hia 6

根据您的意思,绑定不是以兼容的方式配置的.

我怀疑WCF主机已经wsHttpBinding和您的客户端有BasicHttpBinding或类似...

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