Is it possible to configure the WCF service using the freely managed Windsor configuration without config or svc files?

I have an ASP.Net MVC 3.0 web application hosted on IIS and I am using Castle Windsor version 3.0.

What I would like to do is register the WCF service with webHttpBinding without any entries in the web.config file or with the .svc file. Is it possible?

I tried this in my implementation of IWindsorInstaller:

container.AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero); container.Register( Component .For<IMyService>() .ImplementedBy<MyService>() .AsWcfService(new DefaultServiceModel() .AddBaseAddresses("http://localhost/WebApp/Services") .AddEndpoints(WcfEndpoint .BoundTo(new WebHttpBinding()) .At("MyService.serv")) .Hosted() .PublishMetadata())); 

And I ignore everything that ends with such a service in my RegisterRoutes method in the global asax:

 routes.IgnoreRoute("{resource}.serv/{*pathInfo}"); 

If I point the browser to http: //localhost/WebApp/Services/MyService.serv , I get 404.

What am I doing wrong, or am I trying to do something stupid (or not possible, or both!)?

+4
source share
1 answer

Thanks to Ladislav’s proposal to use ServiceRoute, I developed how to do it, but I'm not sure that it is perfect, here’s what I did (in case the search engines find it and can improve it, etc.):

Created an extension method for ComponentRegistration as follows:

 public static ComponentRegistration<T> AddServiceRoute<T>( this ComponentRegistration<T> registration, string routePrefix, ServiceHostFactoryBase serviceHostFactory, string routeName) where T : class { var route = new ServiceRoute("Services/" + routePrefix + ".svc", serviceHostFactory, registration .Implementation .GetInterfaces() .Single()); RouteTable.Routes.Add(routeName, route); return registration; } 

What this means is that it adds a service route, which places the service in the Services folder and overlayes the .svc extension (I will probably delete this). Notice, I assume that the service implements only one interface, but in my case this is normal, and I think this is good practice.

I'm not sure if this is the best place to use this extension method, or even if the extension method is really necessary at all - maybe I should do it with the help of the service host creator or something else, I don’t know

Then, in MapRoute calls, I definitely added this to the restriction parameter in accordance with the question Routing MVC2 with WCF ServiceRoute: Html.ActionLink displaying invalid links!

 new { controller = @"^(?!Services).*" } 

It just does everything that starts Services that cannot be negotiated as a controller. I don’t really like it, because I have to add it to all my routes - I would prefer to globally make the Services folder into the service converter or something (I don’t know enough about MVC!).

Finally, in my windsor installer, I will register the service as follows:

 container.AddFacility<WcfFacility>( f => { f.Services.AspNetCompatibility = AspNetCompatibilityRequirementsMode.Allowed; f.CloseTimeout = TimeSpan.Zero; }); container.Register( Component .For<IMyService>() .ImplementedBy<MyService>() .AsWcfService(new DefaultServiceModel() .AddEndpoints(WcfEndpoint.BoundTo(new WebHttpBinding())) .Hosted() .PublishMetadata()) .AddServiceRoute("MyService", new DefaultServiceHostFactory(), null)); 

After that, I can go to the service and pick it up and build it perfectly!

As I said, this may not be the best way to do this, but it works.

+7
source

Source: https://habr.com/ru/post/1390455/


All Articles