Tôi đang gặp sự cố khi tiêm các dịch vụ phụ thuộc vào dịch vụ WCF của tôi bằng Autofac 1.4.5. Tôi đã đọc và theo dõi các Autofac wiki page on WcfIntegration nhưng gỡ lỗi của tôi cho thấy rằng dịch vụ WCF của tôi được tạo ra bởi phương pháp System.ServiceModel.Dispatcher.InstanceBehavior.GetInstance()
và không phải bởi AutofacWebServiceHostFactory
. Tôi đang làm gì sai?Sử dụng Autofac cho DI vào dịch vụ WCF được lưu trữ trong ứng dụng ASP.NET
tôi đã thiết lập ajax.svc
tập tin của tôi trông giống như một trong những ví dụ để sử dụng với WebHttpBinding
:
<%@ ServiceHost Language="C#" Debug="true"
Service="Generic.Frontend.Web.Ajax, Generic.Frontend.Web"
Factory="Autofac.Integration.Wcf.AutofacWebServiceHostFactory,
Autofac.Integration.Wcf" %>
My lớp dịch vụ WCF Ajax
được định nghĩa như thế này:
namespace Generic.Frontend.Web
{
[ServiceContract]
[AspNetCompatibilityRequirements(
RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Ajax
{
public MapWebService MapWebService { get; set;}
public Ajax() {
// this constructor is being called
}
public Ajax(MapWebService mapWebService)
{
// this constructor should be called
MapWebService = mapWebService;
}
[WebGet(ResponseFormat = WebMessageFormat.Json)]
[OperationContract(Name = "mapchange")]
public MapChangeResult ProcessMapChange(string args)
{
// use the injected service here
var result = MapWebService.ProcessMapChange(args);
return result;
}
}
}
Bây giờ Tôi đã sử dụng hệ thống dây điện trong số Global.asax.cs
như được hiển thị trong wiki được đề cập ở trên:
var builder = new ContainerBuilder();
builder.RegisterModule(new AutofacModuleWebservice());
var container = builder.Build();
AutofacServiceHostFactory.Container = container;
.210
với
class AutofacModuleWebservice : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.Register<Ajax>();
builder.Register<MapWebService>().ContainerScoped();
}
}
Trong web.config của tôi, tôi có
<services>
<service name="Generic.Frontend.Web.Ajax">
<endpoint address="http://mysite.com/ajax.svc/" binding="webHttpBinding"
contract="Generic.Frontend.Web.Ajax" />
</service>
</services>
.
Dịch vụ đã hoạt động tốt nhưng tôi không thể nhận được các bit Autofac (đọc: tạo/tiêm) để hoạt động. Bất kỳ ý tưởng?
Edit: Loại bỏ các constructor mặc định không may dẫn đến ngoại lệ sau đây:
System.InvalidOperationException:
The service type provided could not be loaded as a service because it does not
have a default (parameter-less) constructor. To fix the problem, add a default
constructor to the type, or pass an instance of the type to the host.
Chúc mừng, Oliver
Bạn đã giải quyết vấn đề này chưa? Tôi có vấn đề tương tự và tôi đã đặt câu hỏi của mình ở đây -> http://stackoverflow.com/questions/16489403/using-autofac-with-ajax-enabled-wcf-service – codingjoe
Thật không may, không. Đó cũng là lý do tại sao không có gì được đánh dấu là câu trả lời, tại đây: - | – Oliver
Làm thế nào để bạn lưu trữ nó? iis, là, tự lưu trữ? Bạn nên viết nó xuống trong câu hỏi của bạn tôi nghĩ. Bạn viết mã khác nhau tùy thuộc vào điều này. – Johan