Problem creating WCF service with Windsor - wcf

Does anyone know how to configure WCF using Windsor on IIS 7.0? I'm using the latest from WCF Windsor facility trunk and Windsor 2.1.1. The example on http://www.castleproject.org/container/facilities/trunk/wcf/index.html is out of date. Even demo project in WCF facility doesn't mention how to setup WCF service in IIS using the config and I couldn't find any example where I can setup WCF on server side using system.serviceModel section of web.config or even through code. When I use the following code it always creates basicHttpBinding and I couldn't figure out how to setup different bindings.
protected void Application_Start(object sender, EventArgs e)
{
var returnFaults = new ServiceDebugBehavior
{
IncludeExceptionDetailInFaults = true,
HttpHelpPageEnabled = true
};
var metadata = new ServiceMetadataBehavior {HttpGetEnabled = true};
container = new WindsorContainer()
.AddFacility<WcfFacility>()
.Register(
Component.For<IServiceBehavior>().Instance(returnFaults),
Component.For<IServiceBehavior>().Instance(metadata),
Component.For<IMyService>()
.Named("MyService")
.ImplementedBy<MyService>()
.LifeStyle.Transient
);
}
And here is MyService.svc
<%# ServiceHost Language="C#" Debug="true" Service="MyService"
Factory="Castle.Facilities.WcfIntegration.DefaultServiceHostFactory, Castle.Facilities.WcfIntegration" %>

I recently wrote a blog post about Windsor's WCF Facility. Be sure to read the comments as well, as they include a discussion involving one of Windsor's active committers; they should give you a pretty good impression of the future direction.

Related

Hosting WCF service on linux

Is there Any way of hosting WCF service on Linux.
I read about wine but i didn't see any example of hosting WCF service with it.
P.S : I have tried mono and mod_mono but to no avail.
You can host it in a stand-alone console application like so:
using System;
using System.ServiceModel;
using Service;
namespace Host
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine ("WCF Host!");
var binding = new BasicHttpBinding ();
var address = new Uri ("http://localhost:8080");
var host = new ServiceHost (typeof(GreeterWcfService));
host.AddServiceEndpoint (
typeof(IGreeterWcfService), binding, address);
host.Open ();
Console.WriteLine ("Type [Enter] to stop...");
Console.ReadLine ();
host.Close ();
}
}
}
Where GreeterWcfService is the WCF service class itself and IGreeterWcfService is the service contract.
Full working example solution in GitHub - with separate projects for the service, the hosting and a client. Check it out.
Its possible but you should refer to this link for understanding current state and known issues - http://www.mono-project.com/docs/web/wcf/. It's limited now. For eg. if you wish to use WSHttpBinding its not supported currently.

Configuring a WCF and StructureMap ServiceHostFactory from code

I am creating a custom ServiceHost object and configuring it from code. My service is using InstanceContextMode.Single and ConcurrencyMode.Multiple and is hosted in a windows service.
As stated in a number of blogs/articles (here), sharing a StructureMap container across instances requires using a custom InstanceProvider, ServiceBehavior and ServiceHostFactory.
My initialization code looks like this. I do not use a config file.
var baseAddress = ConfigurationManager.AppSettings["BaseAddress"];
var port = Int32.Parse(ConfigurationManager.AppSettings["Port"]);
Host = new MyServiceHost(typeof(MediaFileServicePrivate), new Uri(string.Format(baseAddress, port)));
var binding = new NetTcpBinding();
Host.AddServiceEndpoint(typeof(IMediaFileServicePrivate), binding, string.Format(baseAddress, port));
How do I tell the service to use my custom service host factory? All the examples I can find configure it from the config file.
Is a ServiceHostFactory only used for IIS/WAS hosted scenarios? If so, how do I use SM for a self-hosted InstanceContextMode.Single service?
Has this not been answered? essentially you tell it to use your servicefactory in the wcf markup
<%# ServiceHost Language="C#" Debug="true" Service="WcfWithDI.Service1" CodeBehind="Service1.svc.cs" Factory="WcfWithDI.MyServiceFactory"%>
I have a sample project here all wired up with a custom provider (and structuremap)
https://github.com/billCreativeD/WCF_With_DI
Unfortunately it makes little sense to say "the service uses the factory". You would use the Ninject factory to create your service:
var factory = new NinjectServiceHostFactory();
var address = new Uri(_baseAddress, path);
ServiceHostBase host = factory.CreateServiceHost(typeName, new[] {address});
var binding = new NetTcpBinding();
host.AddServiceEndpoint(typeof(TheType), binding, string.Format(baseAddress, port));
The directive in the .svc file is used to tell .NET how to instantiate your service.

WCF: how to restrict using some protocols?

WCF (winodws service hosting) service uses set of protocols and bindings: http, https, net.tcp, net.pipe.
It uses config file settings.
I want to build demo version of the service.
This demo will use only net.pipe protocol.
How I can restrict service to use only this one?
I can do changes in code , but how and where?
ServiceHost owns collection of ChannelDispatchers in ChannelDispatchers property. You can use ChannelDispatcher.BindingName to figure out name of binding used in your service.
ServiceHost host = new ServiceHost(typeof(SomeService), baseAddress))
//configure service endpoints here
host.Open();
#if DEMO_MODE
foreach (ChannelDispatcher dispatcher in host.ChannelDispatchers)
{
//binding name includes namespace. Example - http://tempuri.org/:NetNamedPipeBinding
var bindingName = dispatcher.BindingName;
if (!(bindingName.EndsWith("NetNamedPipeBinding") || bindingName.EndsWith("MetadataExchangeHttpBinding")))
throw new ApplicationException("Only netNamedPipeBinding is supported in demo mode");
}
#endif

how can I add a JSONP endpoing for WCF Ria Services to enable cross-domain calls?

I'm aware that WCF RIA Services has a Microsoft.ServiceModel.DomainServices.Hosting.JsonEndpointFactory that I can use to enable JSON.
I need to enable cross-domain calls via JSONP. Is there an existing DomainServiceEndpointFactory that will accomplish this?
I just needed to do this - I overrode JsonEndpointFactory and tinkered with the binding in there, then added an endpoint using the new class.
namespace Bodge
{
public class JsonPEndpointFactory : JsonEndpointFactory
{
public override IEnumerable<ServiceEndpoint> CreateEndpoints(DomainServiceDescription description, DomainServiceHost serviceHost)
{
IEnumerable<ServiceEndpoint> endPoints = base.CreateEndpoints(description, serviceHost);
foreach (ServiceEndpoint endPoint in endPoints)
{
if (endPoint.Binding is WebHttpBinding)
{
((WebHttpBinding)endPoint.Binding).CrossDomainScriptAccessEnabled = true;
}
}
return endPoints;
}
}
}
<endpoints>
<add name="JSONP" type="Bodge.JsonPEndpointFactory, Bodge, Version=1.0.0.0"/>
</endpoints>
Then access your service with the endpoint and the callback query param e.g.
http://blah/service.svc/JSONP/GetStuff?callback=callbackname
Hope that helps,
Chris.
Formatting in comments isn't great, so for future reference here are the required usings and assemblies.
Thanks very much, that's exactly what I needed!for future reference, these are the using statements:
Namespaces:
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using Microsoft.ServiceModel.DomainServices.Hosting;
Assemblies
NETFX 4.0
System.ServiceModel
System.ServiceModel.Web
WCF RIA Services V1.0 SP2 RC
System.ServiceModel.DomainServices.Hosting
System.ServiceModel.DomainServices.Server
WCF RIA Services Toolkit (September 2011)
Microsoft.ServiceModel.DomainServices.Hosting

Autofac WCF integration + sessions

I am having an ASP.NET MVC 3 application that collaborates with a WCF service, which is hosted using Autofac host factory. Here are some code samples:
.svc file:
<%# ServiceHost
Language="C#"
Debug="true"
Service="MyNamespace.IMyContract, MyAssembly"
Factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf" %>
Global.asax of the WCF service project:
protected void Application_Start(object sender, EventArgs e)
{
ContainerBuilder builder = new ContainerBuilder();
//Here I perform all registrations, including implementation of IMyContract
AutofacServiceHostFactory.Container = builder.Build();
}
Client proxy class constructor (MVC side):
ContainerBuilder builder = new ContainerBuilder();
builder.Register(c => new ChannelFactory<IMyContract>(
new BasicHttpBinding(),
new EndpointAddress(Settings.Default.Url_MyService)))
.SingleInstance();
builder.Register(c => c.Resolve<ChannelFactory<IMyContract>>().CreateChannel())
.UseWcfSafeRelease();
_container = builder.Build();
This works fine until I want WCF service to allow or require sessions ([ServiceContract(SessionMode = SessionMode.Allowed)], or [ServiceContract(SessionMode = SessionMode.Required)]) and to share one session with the MVC side. I changed the binding to WSHttpBinding on the MVC side, but I am having different exceptions depending on how I tune it.
I also tried changing AutofacServiceHostFactory to AutofacWebServiceHostFactory, with no result.
I am not using config file as I am mainly experimenting, not developing real-life application, but I need to study the case. But if you think I can achieve what I need only with config files, then OK, I'll use them.
I will provide exception details for each combination of settings if required, I'm omitting them not to make the post too large. Any ideas on what I can do?