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

时间:2011-08-17 16:44:07

标签: wcf endpoint

我正在松散地遵循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;服务http://localhost:8090/EmployeeService不支持charset = utf-8。客户端和服务绑定可能不匹配。 ---&GT; System.Net.WebException:远程服务器返回错误:(415)无法处理消息,因为内容类型为'text / xml; charset = utf-8'不是预期的类型'application / soap + xml;字符集= UTF-8' ..

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

1 个答案:

答案 0 :(得分:6)

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

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

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