简体   繁体   English

创建一个WCF Web Api客户端

[英]Creating a WCF Web Api Client

I've been using the WCF Web Api recently and i've been using the WCF Web API Test Client that is built in to it to test my created webservices. 我最近一直在使用WCF Web Api,并且一直在使用内置的WCF Web API测试客户端来测试我创建的Web服务。

I am wanting to create a proxy in code built off of the interface rather than run svcutil.exe to create a proxy. 我想在基于接口构建的代码中创建代理,而不是运行svcutil.exe来创建代理。

My webservice is working fine, however when I use fiddler to examine the message that is sent, it is putting in a namespace into the xml message. 我的网络服务运行良好,但是当我使用提琴手检查发送的消息时,它会将名称空间放入xml消息中。

Below is the code I use to send the request. 以下是我用来发送请求的代码。

        RegisterRequest registerRequest = new RegisterRequest
                                              {
                                                  Email = "test@test.com",
                                                  Firstname = "firstname",
                                                  Lastname = "lastname",
                                                  Password = "password"
                                              };


        var factory = new ChannelFactory<IAccountApi>(new WebHttpBinding(), "http://localhost/WebServices/api/account");
        factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
        var proxy = factory.CreateChannel();
        proxy.Register(registerRequest); 

This request below is generated via the client, and it fails, returning a 500 internal server error 以下请求是通过客户端生成的,失败了,返回500内部服务器错误

<RegisterRequest xmlns="http://schemas.datacontract.org/2004/07/ServiceModel.Accounts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Email>test@test.com</Email><Firstname>firstname</Firstname><Lastname>lastname</Lastname><Password>password</Password></RegisterRequest>

Using the same headers when I send using the api test client the following message passes 当我使用api测试客户端发送邮件时,使用相同的标头会传递以下消息

<RegisterRequest><Email>test@test.com</Email><Firstname>firstname</Firstname><Lastname>lastname</Lastname><Password>password</Password></RegisterRequest>

The only difference being the namespace has been removed. 唯一的区别是名称空间已被删除。

Some final points, 最后几点

1) If I were able to remove this namespace the request would work 1)如果我能够删除此命名空间,则请求将起作用

2) I am not sure if ChannelFactory can be used in conjunction with WCF Web Api. 2)我不确定ChannelFactory是否可以与WCF Web Api一起使用。 The reason being http://wcf.codeplex.com/releases/view/73423 states "[ServiceContract] is no longer required on the Web API class definition", yet Channel Factory requires it. 原因是http://wcf.codeplex.com/releases/view/73423指出“ Web API类定义不再需要[ServiceContract]”,但是Channel Factory需要它。

3) All the examples so far from the WCF Web API look like the following 3)到目前为止,WCF Web API的所有示例如下所示

HttpClient client = new HttpClient(); HttpClient客户端=新的HttpClient();

Contact contact = new Contact() { Name = name };
var response = client.Post("http://localhost:9000/api/contacts/",
    new ObjectContent<Contact>(
        contact, JsonMediaTypeFormatter.DefaultMediaType));

Should I be using HttpClient for my requests instead of channel factory? 我应该使用HttpClient代替通道工厂吗?

Regards, Andrew 问候,安德鲁

It appears that the IAccountApi, which you do not show, is defining a namespace for the service contract. 似乎没有显示的IAccountApi正在为服务合同定义名称空间。 If you really want an empty namespace (not best practice) try something like this: 如果您确实想要一个空的名称空间(不是最佳实践),请尝试以下操作:

 [ServiceContract(Namespace="")]
 public interface IAccountApi
 { ... }

If the namespace is not defined for IAccountApi, check the [DataContract] of RegisterRequest. 如果未为IAccountApi定义名称空间,请检查RegisterRequest的[DataContract]。

I ended up using HttpClient class, it allows GET, POST, PUT and DELETE which was fine for WCF Web API (now called http://www.asp.net/web-api ) 我最终使用了HttpClient类,它允许GET,POST,PUT和DELETE,这对于WCF Web API(现在称为http://www.asp.net/web-api )来说是很好的选择

http://msdn.microsoft.com/en-us/library/system.net.http.httpclient.aspx http://msdn.microsoft.com/zh-CN/library/system.net.http.httpclient.aspx

as far as building a rest proxy using codegen or being dynamic see this article ReST Proxy Object Generator 至于使用codegen或动态创建rest代理,请参阅本文ReST代理对象生成器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM