Wcf dependency injection - wcf

I have an IService.
It is implemented by Service1.
I bind IService to Service1 (im using ninject).
Can I have a .svc file that in the markup has...
Service="IService"
And tell wcf to somehow resolve that service and use it?

No, the .svc file is bound to the service type. What you can have is a route (if you use the ASP.NET Routes integration) where you, in code, resolve the IService binding to Service1 and add the route accordingly.

in the .svc file you can set Factory= to the class that you want to resolve the service. I have not tried setting Service to an interface. If it does not work you can use an abstract base class for your IOC.

Related

How to do dependency injection in a WCF application

I have an IIS-hosted WCF application. Right now the service has this constructor:
public ClassService()
: this(new ClassRepository())
{
}
public ClassService(IClassRepository repository)
{
_Repository = repository;
}
The parameterless constructor is because WCF requires you to have a parameterless constructor when generating service proxies. Now, when I have the service proxy in the UI assembly, the constructor with the IClassRepository is not present so I can't inject an implementation of the repository.
How is this commonly done? One idea I have is that injection would take place not in the UI but in the Service but I am not sure if this would have some repercussions or just plain bad. Can someone give me some ideas?
What you need to do is implement a WCF InstanceProvider, delegating creation requests to your container of choice. Once you have your instance provider coded, you can install it in a ServiceHost by implementing a WCF service behavior. The service behavior, in turn, is installed by adding it to the collection ServiceHostBase.Description.Behaviors.
Here's an MSDN Magazine article on WCF extensibility.

Could not find endpoint element with name ' and contract 'I' in the ServiceModel client configuration section.

I am working on a Base WCF Service- Client WCF service - Consumer Base Wcf servcie model as following:
In this model, I have created a Base WCF service and created 1 WCF service i.e; ClientWCFService and 1 ASMX service i.e; ClientASMXservice using the BaseWCFServiceProxy.cs the Proxy class of Base WCF Service using SVCUtil.exe.
The ClientWCFService and ClientASMXservice are working fine in StandAlone environment.
Now, I created a Consumer Console Application using the same proxy class BaseWCFServiceProxy.cs to access both of the ClientWCFService and ClientASMXservice using the BaseWcfService class.
As per the OOP rules, BaseWcfService class is the base class for the ClientWCFService and ClientASMXservice and i can access these Services using the Base service class constructor.
The service calls are as following:
for ClientWCFService
ModelWcfServiceContractClient _client = new ModelWcfServiceContractClient("IModelWcfServiceContract","http://localhost:64242/ClientWCFServiceWcfUsingSVCProxy.svc");
for ClientASMXservice
ModelWcfServiceContractClient _client = new ModelWcfServiceContractClient("IModelWcfServiceContract","http://localhost:64396/ClientASMXServiceWcfUsingSVCProxy.asmx");
The Consumer Console application is building fine, but at run time while initializing the ModelWCFServiceContractClient object,
it throws the InvalidOperation Exception as :
Could not find endpoint element with name 'IModelWcfServiceContract'
and contract 'IModelWcfServiceContract' in the ServiceModel client
configuration section. This might be because no configuration file was
found for your application, or because no endpoint element matching
this name could be found in the client element.
I have tried using different endpoints for every type of service to resolve this issue but could not get success.
A faster response would be Appreciated. Also please don't help me by just clearing spelling mistakes because it will waste my time to read the Alert that would be generated due to the clearing spell mistakes. Time is critical for me..
Thanks
Try this (hopefully you can follow):
In Visual Studio, Select the project that contains the ServiceReference for your ModelWcfProxy (the one that generated the ModelWcfServiceContractClient proxy class).
Choose, "Show all files" from the Solution Explorer menu bar.
Expand the Service References folder and the generated Service Reference. Find the .svcmap file, look underneath to find the Reference.cs file.
On the top of your generated interface there should be a System.ServiceModel.ServiceContractAttribute defined. In the attribute constructor, you should see a ConfigurationName property being set. This contract configuration name should match the name defined in config or that you are sending in to your method:
ModelWcfServiceContractClient _client = new ModelWcfServiceContractClient("IModelWcfServiceContract","http://localhost:64242/ClientWCFServiceWcfUsingSVCProxy.svc");
Hope this helps.

WCF service hosted in a managed Windows service connect using a WCF service application

Thank you in advance for any advice. I have a Windows service that is hosting a WCF service through net.tcp and this is working great. I have also created a WCF service application. I am trying to add the net.tcp service reference to the service application. Then I add it to the GAC that goes ok but if I try to RegAsm the WCF service application to allow it to be called from Server.CreateObject I get the error:
Warning: Type library exporter encountered a type that derives from a
generic class and is not marked as
[ClassInterface(ClassInterfaceType.None)]. Class interfaces cannot be
exposed for such types. Consider marking the type with
[ClassInterface(ClassInterfaceType.None)] and exposing an explicit
interface as the default interface to COM using the
ComDefaultInterface attribute.
It does not work. I have tried to call it through a class library but this does not work either as the end point is not set correctly.
Any advice?
Ok Fixed this. Was a Permissions Error the iusr account did not have access to the web.config file from the WCF Service Application. So It works now. Now another problem has arised. I need to be able to call the WCF Service Application from asp classic but it does not use a web.config.

WCF CallbackContract requires generated Interface instead of the one I specified in ServiceContract?

I have a Service that does a typical implementation of a CallbackContract like the following:
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(INerveCenterCallBack))]
When I Add a ServiceReference to my WCF Service all works fine. Except when I open the Reference.cs that is generated (ie the auto-generated proxy) I notice that it does this:
CallbackContract=typeof(Synapse.NerveCenter.NerveCenterRef.INerveCenterServiceCallback)
It basically injects both the word "Service" and whatever the namespace I gave the ServiceReference itself at the time of creation "ie NerveCenterRef"
The problem now is when I attempt to connect to my Web Service it Requires that my InstanceContext use a concrete class that inherits from the Generated "NerveCenterRef.INerveCenterServiceCallback" instead of INerveCenterCallBack which I have shared in both a project used by both Client and Server?
Anyone experience the same or know of hints/why's this is occuring?

How do I specify a contract's namespace in the XML configuration of a WCF endpoint?

I've got this WCF service contract (heavily simplified, but pay attention to the namespace it's in):
namespace Foo.Services.BarService
{
[ServiceContract]
interface BarContract {... }
}
In my app.config (client side), I configure an endpoint for some service:
<endpoint address="..."
binding="..."
contract="Foo.Services.BarService.BarContract" />
However, this results in an error saying that no endpoint was found in the client's configuration that supports BarService.BarContract. I can only get rid of this error by changing the contract attribute value to BarService.BarContract (i.e. by removing the namespace).
Why is that? Where could this error come from? Why must I not mention the namespace part of a contract type? Shouldn't that result even more in WCF not finding a matching endpoint?
Reply to the questions in #Ladislav Mrnka's comment below:
I am talking about the client side. (I forgot to mention this bit; sorry for that.)
Can this error possibly come from the server side?
I generated the above service contract, along with a BarClient class that implements it, via Visual Studio's Add Service Reference facility. I specified the URL of the BarService, which is run by someone else. That's where I also specified that the service should be put in the Foo.Services.BarService namespace.
I was going to use the service directly via the BarClient class auto-generated for me, not via a ChannelFactory<BarContract>.
Creating client by Add Service reference does not recreate namespace structure from service. All created types and contracts are placed into new namespace defined by the name of the service reference. So I guess you named your service reference BarService. Client configuration must follow names of generated contracts.