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

发布于 2024-11-29 20:13:05 字数 1557 浏览 0 评论 0原文

我大致遵循 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);
    }
}

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

我目前正在尝试这样调用它们:

serviceHost = SelfServiceHost.StartupService();

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

EmployeeResponse employeeResponse = employeeClient.SaveOrUpdateEmployee(emp);

当我这样做时,我收到此异常:

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

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

I am loosely following the method in WCF The Right Way ... The Manual Way to setup my WCF Service.

I have a manually generated proxy class that looks like this:

// 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);
    }
}

I then want to call some of these services. But I don't want to use any config files (I am setting up some integration tests and I don't want more dependencies than needed.)

I am currently trying to call them like this:

serviceHost = SelfServiceHost.StartupService();

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

EmployeeResponse employeeResponse = employeeClient.SaveOrUpdateEmployee(emp);

When I do that I am getting this exception:

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'..

What do I need to do to get a call to my service working with code only?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白鸥掠海 2024-12-06 20:13:06

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

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

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

From what you dessribe the binding is not configured in a compatible way.

I suspect that the WCF host has wsHttpBinding and your client-side has BasicHttpBinding or similar...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文