我正在编写一个使用Microsoft.ServiceBus.dll 1.0.0.0(.NET 3.5版本)的POC应用程序。
我的WCF合同和服务如下所示:
[ServiceContract(Name="MyServiceContract", Namespace = "http://mydomain.com/")]
internal interface IServiceContract
{
[WebInvoke(Method = "POST", UriTemplate = "/DoOperation")]
[OperationContract]
Stream RelayRequest(Stream requestBody);
}
[ServiceBehavior(Name = "Service1", Namespace = "http://mydomain.com/Service1/", InstanceContextMode = InstanceContextMode.Single)]
internal class Service1 : IServiceContract
{
Stream RelayRequest(Stream requestBody)
{
var contents = GetJsonResponse();
var responseStream = new MemoryStream();
var streamWriter = new StreamWriter(responseStream);
streamWriter.AutoFlush = true;
var writer = new JsonTextWriter(streamWriter);
var serializer = new JsonSerializer();
serializer.Serialize(responseStream, contents);
responseStream.Position = 0 // reset the position of the stream so that it's contents will be read from the beginning.
//Problem Line:
WebOperationContext.OutgoingResponse.ContentType = "application/json";
return responseStream;
}
}
侦听终点配置为使用WebHttpRelayBinding:
当我尝试将传出响应的ContentType分配给“application / json”时,不会发生错误,但调用请求将返回状态码504(网关超时)。
如果我将ContentType更改为“text / javascript”,则调用请求将返回200(OK)。
有些注意事项:
为什么会发生这种情况?我该如何解决这个问题?
编辑:504状态代码可能是由我正在测试的fiddler 推断的红色鲱鱼。 从System.Net.Http.HttpClient发送相同的请求表示在收到响应之前连接已关闭。
编辑:将内容类型设置为几乎所有其他内容(包括非敏感值)。我可以解决的唯一内容类型是application / json
答案 0 :(得分:-1)
这就是我使它工作的方式,但它是一个在IIS本地托管的简单WCF服务。我不使用Microsoft.ServiceBus或Azure。
第一个提琴手的回应。如果这是您正在寻找的,那么继续编码:)
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 121
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu, 04 Apr 2013 17:17:05 GMT
[{"BoolValue":true,"StringValue":"blah"},{"BoolValue":false,"StringValue":"boo"},{"BoolValue":true,"StringValue":"floo"}]
服务定义:
namespace yourNS
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "/CT", ResponseFormat = WebMessageFormat.Json)]
List<CompositeType> GetData();
}
[DataContract]
public class CompositeType
{
[DataMember]
public bool BoolValue { get; set; }
[DataMember]
public string StringValue { get; set; }
}
public class Service1 : IService1
{
public List<CompositeType> GetData()
{
return new List<CompositeType>
{
new CompositeType { BoolValue = true, StringValue = "blah" },
new CompositeType { BoolValue = false, StringValue = "boo" },
new CompositeType { BoolValue = true, StringValue = "floo" },
};
}
}
}
和web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="yourNS.Service1">
<endpoint
address=""
binding="webHttpBinding"
behaviorConfiguration="MyBehave"
contract="yourNS.IService1" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="MyBehave">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>