我正在尝试使用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 Server应用程序调用。这些都使用相同的代码库来拨打电话。
据我所知,两者的配置相同。
<system.serviceModel>
<endpointBehaviors>
<behavior name="WcfRestBehavior">
<webHttp />
</behavior>
<client>
<endpoint name="ServiceAgreementRequestService" address="http://PLACEHOLDER/" binding="webHttpBinding" behaviorConfiguration="WcfRestBehavior" contract="IServiceAgreementRequestService"/>
答案 0 :(得分: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();
}
答案 1 :(得分: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;
}