c# - WCF Rest 客户端发送不正确的内容类型

标签 c# json wcf rest coldfusion

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

这是服务契约(Contract)。如您所见,我们专门使用 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
Expect: 100-continue
Accept-Encoding: gzip, deflate

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

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

编辑:

在底层,GetServiceClient(clientId) 调用使用 system.servicemodel.clientbase 和 ChannelFactory 来创建通信 channel 。我们调用的端点由客户端更改,因此我们在这些代码之上有一些代码来动态更改端点。

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

据我所知,两者的配置相同。

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

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

最佳答案

要在服务中使用 WCF REST 客户端,您需要使用类似于以下代码的代码创建新的操作上下文范围。

调用代码:

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

关于c# - WCF Rest 客户端发送不正确的内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12975524/

相关文章:

javascript - 如何从 JSON 数组中删除重复的对象

java - 使用 Gson 创建 JSON 时 POJO 中黑色字段的影响

.net - 如何在本地计算机上模拟 CORS?

c# - 读取文件时我得到 ???作为输入而不是西里尔字母

c# - EmptyResult 上的 jQuery AJAX 请求炸弹

c# - CLSCompliant 属性是否关心旧版本的 .NET 语言?

c# - WinRT 创建一个文件(如果该文件不存在)并知道该文件已创建

c# - ASP.net Core 2.0 中的 appsettings.json 预览配置 GetSection null

wpf - 如何在 WCF 客户端中接受自签名 SSL 证书?

c# - WCF REST、流式上传文件和 httpRuntime maxRequestLength 属性