本文介绍了WCF Rest客户端发送不正确的内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用wcf客户端发送请求到使用json的ColdFusion 9服务。但是,请求的内容类型是xml。



这是服务合同。正如你可以看到我们使用Json的RequestFormat特定。

  [ServiceContract(Name =ServiceAgreementRequestService,Namespace = NetworkNamespaces.ServiceNamespace) ] 
public interface IServiceAgreementRequestService
{
[OperationContract]
[FaultContract(typeof(RequestFault))]
[WebInvoke(UriTemplate =?method = CreateUser,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]
CreateUserResponse CreateUser(CreateUserRequest request);
}



我还试图在OutGoing请求上设置Request.ContentType,也没有工作。

  using(var context = this.GetServiceClient(clientId))
{
WebOperationContext.Current.OutgoingRequest.ContentType =application / json; charset = UTF-8;
var request = new CreateUserRequest(user.Id,user.Person.FirstName,user.Person.LastName);
var response = context.Channel.CreateUser(request);
}

这是发送的请求

  POST http://somesite.domain.com/WebServices/ProviderService.cfc/?method=CreateUser HTTP / 1.1 
Content-Type:application / xml ; charset = utf-8
VsDebuggerCausalityData:uIDPo7eh9U9jsBVLqVgGtqTK + eMBAAAAb ++ 0xkOSQEmcAKZLgQEsp2 / muY2ca6NJiul6pkAaWZwACQAA
Host:somehost.domain.com
Content-Length:58
预期:100-继续
Accept-Encoding:gzip,deflate

{UserId:4,FirstName:FirstName,LastName:LastName}



如何让这个使用正确的内容类型?



/ p>

在引擎下,GetServiceClient(clientId)调用使用system.servicemodel.clientbase和ChannelFactory创建通信通道。我们呼叫客户端更改的端点,所以我们有一些代码,以动态更改端点。



一些更多的信息。我们有两个应用程序:一个是.net MVC 4 Web应用程序来托管客户端应用程序,一个是.net WCF服务器应用程序来托管后端服务。我可以从Web应用程序成功调用ColdFusion应用程序,但不能调用wcf Server应用程序。这两个都使用相同的代码库进行呼出。



我可以告诉配置是一样的。

 < system.serviceModel> 
< endpointBehaviors>
< behavior name =WcfRestBehavior>
< webHttp />
< / behavior>

< client>
< endpoint name =ServiceAgreementRequestServiceaddress =http:// PLACEHOLDER /binding =webHttpBindingbehaviorConfiguration =WcfRestBehaviorcontract =IServiceAgreementRequestService/&


解决方案

要在服务中使用WCF REST客户端



调用代码

$需要创建一个新的操作上下文范围b
$ b

  var client = this.GetServiceClient(clientId); 
using(new OperationContextScope(client.InnerChannel))
{
var request = new CreateUserRequest(user.Id,user.Person.FirstName,user.Person.LastName);
var response = client.CreateUser(request);
}

其他实现



class MyType:ClientBase< IServiceClient>,IServiceClient
{
public MyType():base(ServiceAgreementRequestService){}
public CreateUserResponse CreateUser(CreateUserRequest req)
{
return this.Channel.CreateUser(req);
}
}

public MyType GetServiceClient(int clientId)
{
return New MyType();
}


I am trying to send a request using a wcf client to a ColdFusion 9 service using json. However, the content-type of the request is for xml.

Here is the service contract. As you can see we specificy using RequestFormat of json.

[ServiceContract(Name = "ServiceAgreementRequestService", Namespace = NetworkNamespaces.ServiceNamespace)]
public interface IServiceAgreementRequestService
{
[OperationContract]
[FaultContract(typeof(RequestFault))]
[WebInvoke(UriTemplate = "?method=CreateUser", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
CreateUserResponse CreateUser(CreateUserRequest request);
}

I have also tried to set the Request.ContentType on the OutGoing request and this also did not work.

using (var context = this.GetServiceClient(clientId))
{
WebOperationContext.Current.OutgoingRequest.ContentType = "application/json; charset=UTF-8";
var request = new CreateUserRequest(user.Id, user.Person.FirstName, user.Person.LastName);
var response = context.Channel.CreateUser(request);
}

Here is the request that gets sent

POST http://somesite.domain.com/WebServices/ProviderService.cfc/?method=CreateUser HTTP/1.1
Content-Type: application/xml; charset=utf-8
VsDebuggerCausalityData: uIDPo7eh9U9jsBVLqVgGtqTK+eMBAAAAb++0xkOSQEmcAKZLgQEsp2/muY2ca6NJiul6pkAaWZwACQAA
Host: somehost.domain.com
Content-Length: 58
Expect: 100-continue
Accept-Encoding: gzip, deflate

{"UserId":4,"FirstName":"FirstName","LastName":"LastName"}

How do I get this to use the correct content-type?

EDIT:

Under the hood, the GetServiceClient(clientId) call uses system.servicemodel.clientbase and ChannelFactory to create the communication channel. The endpoint that we call out to changes by client so we have some code on top of those to dynamically change out the endpoint.

Some more info. We have two applications : One is a .net MVC 4 web application to host the client app and one is a .net WCF Server application to host the backend services. I can call out to the ColdFusion app successfully from the web application, but not the wcf Server application. These both use the same code base to make the outgoing call.

As far as I can tell the config is the same for both.

<system.serviceModel>
<endpointBehaviors>
<behavior name="WcfRestBehavior">
<webHttp />
</behavior>

<client>
<endpoint name="ServiceAgreementRequestService" address="http://PLACEHOLDER/" binding="webHttpBinding" behaviorConfiguration="WcfRestBehavior" contract="IServiceAgreementRequestService"/>
解决方案

To use the WCF REST Client within a service, you'll need to create a new operation context scope, by using code similar to the one below.

Calling code:

var client = this.GetServiceClient(clientId);
using (new OperationContextScope(client.InnerChannel))
{
    var request = new CreateUserRequest(user.Id, user.Person.FirstName, user.Person.LastName);
    var response = client.CreateUser(request);
}

Other implementations:

class MyType : ClientBase<IServiceClient>, IServiceClient
{
    public MyType() : base("ServiceAgreementRequestService") { }
    public CreateUserResponse CreateUser(CreateUserRequest req)
    {
        return this.Channel.CreateUser(req);
    }
}

public MyType GetServiceClient(int clientId)
{
    return new MyType();
}

这篇关于WCF Rest客户端发送不正确的内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:05
查看更多