3

我正在尝试使用 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
Expect: 100-continue
Accept-Encoding: gzip, deflate

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

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

编辑:

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

更多信息。我们有两个应用程序:一个是托管客户端应用程序的 .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"/>
4

2 回答 2

5

要在服务中使用 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();
}
于 2012-10-19T17:23:59.390 回答
0

请找到一些关于如何使用 WebRequest 调用 WCF 服务的示例代码

var request = WebRequest.Create(string.Concat(serviceUrl, resourceUrl)) as HttpWebRequest;
            if (request != null)
            {
                request.ContentType = "application/json";
                request.Method = method;
            }

            if(method == "POST" && requestBody != null)
            {
                requestBodyBytes = ToByteArrayUsingDataContractSer(requestBody);
                request.ContentLength = requestBodyBytes.Length;
                using (Stream postStream = request.GetRequestStream())
                    postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);                    
            }

            if (request != null)
            {
                var response = request.GetResponse() as HttpWebResponse;
                if(response.StatusCode == HttpStatusCode.OK)
                {
                    Stream responseStream = response.GetResponseStream();
                    if (responseStream != null)
                    {
                        var reader = new StreamReader(responseStream);
                        responseMessage = reader.ReadToEnd();                        
                    }
                }
                else
                {   
                    responseMessage = response.StatusDescription;
                }
            }

    private static byte[] ToByteArrayUsingDataContractSer<T>(T requestBody)
    {
        byte[] bytes = null;
        var serializer1 = new DataContractSerializer(typeof(T));            
        var ms1 = new MemoryStream();            
        serializer1.WriteObject(ms1, requestBody);
        ms1.Position = 0;
        var reader = new StreamReader(ms1);
        bytes = ms1.ToArray();
        return bytes;            
    }
于 2012-10-19T16:59:37.547 回答