将unity.wcf与asp.net路由集成

时间:2013-10-19 20:52:55

标签: c# .net wcf unity-container

我正在尝试在没有svc文件的情况下在我的wcf Web服务中使用unity.wcf。

我的代码:

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)]
//[BasicAuthentication] TODO: this need to be set up
public class SsoAuthenticationService : ISsoAuthenticationService
{
    private readonly ICustomerManager _manager;
    internal const string ServiceUri = "SsoAuthenticationService";

    public SsoAuthenticationService(ICustomerManager manager)
    {
        _manager = manager;
    }

    [WebGet(UriTemplate = "CreateSsoLogin/{request}")]
    public ServiceResponse<bool> CreateSsoLogin(string request)
    {
        // TODO: inputs and outputs are wrong
        _manager.DoWork();
        return new ServiceResponse<bool>();
    }
}

    public class ServiceFactory : UnityServiceHostFactory
{
    protected override void ConfigureContainer(Microsoft.Practices.Unity.IUnityContainer container)
    {
        container.RegisterType<ISsoAuthenticationService, SsoAuthenticationService>("authentication")
            .RegisterType<ICustomerManager, CustomerManager>();


    }
}

现在当我尝试连接ServiceFactory类时出现问题 - 在我看到的所有代码示例中,他们通过svc文件执行此操作,但我的应用程序中没有,因为它使用的是ASP.NET路由。

所以我的Global.asax看起来像:

 private static void RegisterRoutes(RouteCollection routes)
    {

        //Integrate WCF services with the ASP.NET routing feature
        routes.Add(new ServiceRoute(SsoAuthenticationService.ServiceUri, new ServiceFactory(), typeof(SsoAuthenticationService)));
    }

当我调用我的Web服务方法时,我没有访问Web方法代码(它确实调用了ServiceFactory.ConfigureContainer)。如果我没有使用Unity,Global.asax看起来像:

 private static void RegisterRoutes(RouteCollection routes)
    {

        //Integrate WCF services with the ASP.NET routing feature
        routes.Add(new ServiceRoute(SsoAuthenticationService.ServiceUri, new WebServiceHostFactory(), typeof(SsoAuthenticationService)));
    }

如果我将其更改为此,则会触发Web方法,但当然它会抱怨构造函数。

使ServiceFactory类的行为与WebServiceHostFactory类似,需要哪些额外的配置?

或者更好的选择是不使用Unity.Wcf,并尝试使用简单的Unity实现?

1 个答案:

答案 0 :(得分:3)

好的,我已设法使用以下链接http://www.agile-code.com/blog/rest-web-services-with-unity-wcf/

找到答案

我必须创建一对新的类来继承WebServiceHostFactory(abstract)和WebServiceHost,如链接中所述。

一旦这些已经到位,并且我的ServiceFactory类继承自新类,一切都开始有效。