0
最近、RESTful WCFサービスを含むC#.NetソリューションのIoC ContainerとしてAutofacを実装しました。これは、Acceptヘッダーをapplication/xmlに設定することで、XMLで応答を得ることができなくなったことを消費者の一部が気付くまで、かなりうまく機能していたようです。これでAcceptヘッダーに関係なくJSONだけが返されます。承認:RESTful WCFサービスのAutofac実装後にapplication/xmlが機能しなくなる
私はこの問題が、Autofacを実装するのに必要なService.svcファイル内のに置き換えられたと考えています。
誰にでも解決策がありますか?
以下は簡略化されていますが、代表的なコードサンプルです。
Service.svc:
<%@ ServiceHost
Language="C#"
Service="MySolution.MyService, MySolution.MyService"
CodeBehind="WcfServiceImplementations/Service.cs"
Factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf"
%>
IMyService.cs:
...
[ServiceContract(Name = "MyService", Namespace = "WebServices")]
[ServiceKnownType(typeof(Object))]
public interface IMyService
{
/// Comments
[OperationContract]
[WebGet(UriTemplate = "Method/{Id}",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
Object MyMethod(string Id);
...
}
MyService.cs:
...
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class MyService : IMyService
{
private readonly IMyDependency _myDependency;
public MyService (IMyDependency myDependency)
{
_myDependency = myDependency;
}
public Object MyMethod(string Id)
{
// Method code here
}
...
}
のGlobal.asax:
public class Global : HttpApplication
{
private void Application_Start(object sender, EventArgs e)
{
// Autofac initialization is actually in another class, but we call it here like this
var builder = new ContainerBuilder();
builder.RegisterType<MyService>().AsSelf();
builder.RegisterType<MyDependency>().As<IMyDependency>();
var container = builder.Build();
AutofacHostFactory.Container = container;
...
}
}
フィドラーから
生の要求:
GET http://localhost/MySolution/MyService.svc/MyMethod/12345 HTTP/1.1
Accept: application/xml
Authorization: meyer.john
ありがとうございます!これはうまくいった!このファクトリについては、http://docs.autofac.org/en/latest/(2011年9月12日現在)のドキュメントには記載されていません。 –