최근에 RESTful WCF 서비스가 포함 된 C# .Net 솔루션의 IoC 컨테이너로 Autofac을 구현했습니다. 일부 고객은 Accept 헤더를 application/xml로 설정하여 더 이상 XML로 응답을받을 수 없다는 사실을 알게 될 때까지는 꽤 잘 작동하는 것처럼 보였습니다. 이제 Accept 헤더에 관계없이 JSON 만 반환합니다.Accept : RESTful WCF 서비스에서 Autofac 구현 후 application/xml이 더 이상 작동하지 않습니다.
나는 Autofac을 구현하는 데 필요한 Service.svc 파일에서 Factory="System.ServiceModel.Activation.WebServiceHostFactory"
을 Factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf"
으로 바꾸는 데 문제가 있다고 생각합니다.
누구에게도 해결책이 있습니까?
다음은 단순하지만 대표적인 코드 샘플입니다.
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;
...
}
}
,745 피들러에서
원시 요청 :
GET http://localhost/MySolution/MyService.svc/MyMethod/12345 HTTP/1.1
Accept: application/xml
Authorization: meyer.john
감사합니다! 이것은 효과가있다! 이 공장에 대한 언급은 2011 년 9 월 12 일 현재 http://docs.autofac.org/ko/latest/의 설명서에 없습니다. –