I am trying to consume a service in my module in the module web config I added my service config as the following.
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="WSEventSoap" />
<binding name="BasicHttpBinding_ITwitterService" />
<binding name="BasicHttpBinding_ILoyalty">
<security mode="Transport" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint name="BasicHttpBinding_ITwitterService"
address="wwww.mysite.com/MediaServices/TwitterService.svc"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ITwitterService"
contract="TwitterService.ITwitterService" />
</client>
</system.serviceModel>
within the modules web.config. What I am noticing is that
I can't seem to access web config settings in my module
I keep getting the following error.
Could not find default endpoint element that references contract 'TwitterService.ITwitterService' 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 contract could be found in the client element.
Please help.
On the site I'm currently working on, like Betrand suggests, we've avoided modifying the global web.config to ensure the module is portable between projects.
Instead we've just created the Binding and Endpoint in code, rather that them getting pulled from the config. We've added our own setting to the Orchard site settings to let the user specify the endpoint address via the admin dashboard, which is the only part we actually need to be configurable.
To do this, in the module add a service reference as normal, if it adds those sections to your module web.config remove them as you won't be using them.
Then in code do something like
Binding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://localhost:8050/MyService/");
using (var client = new MyServiceClient(binding, endpointAddress))
{
client.MyMethod();
}
There's plenty examples of this on Stackoverflow and elsewhere (e.g. WCF: How can I programatically recreate these App.config values?)
Related
We have some really legacy wcf code and we got a strange problem:
WCFChartService.ChartServiceClient service = new WCFChartService.ChartServiceClient();
using (new OperationContextScope(service.InnerChannel))
{
service.Endpoint.Address = new System.ServiceModel.EndpointAddress(
"http://mypreprodsite.net/MyChartService.svc");
service.UpdateChartTemplate(
xxxx);
}
In web.config, we have:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IChartService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://myprodsite.net/MyChartService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IChartService"
contract="WCFChartService.IChartService" name="BasicHttpBinding_IChartService" />
</client>
</system.serviceModel>
Above code throws some error, so I debugged this, and turned out if in my web.config I have this everything will be fine
<endpoint address="http://mypreprodsite.net/MyChartService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IChartService"
contract="WCFChartService.IChartService" name="BasicHttpBinding_IChartService" />
The function change in wcf has only been deployed to preprod site, so looks like in my testing project, changing url directly in service.Endpoint.Address doesn't work as I expected.
As I moved away from old wcf code years ago (only randomly got assigned to look this one), can someone explain why? If I set service.Endpoint.Address="localhost:8049/MyChartService.svc" and my local machine does get called, so I am rather confused.
There are two ways to specify endpoint addresses for a service in WCF. You can specify an absolute address for each endpoint associated with the service or you can provide a base address for the ServiceHost of a service and then specify an address for each endpoint associated with this service that is defined relative to this base address.
Here is the reference: Specifying an Endpoint Address.
I'm performing what I believed was a pretty basic task. We have an environment with multiple servers (DEV, TEST, PRODUCTION) and I'd like to programmatically change the service endpoint. To do this I am creating a new EndPointAddress and instantiating the client as:
BasicHttpBinding binding = new BasicHttpBinding("BasicHttpBinding_IMyService");
EndpointAddress endpoint = new EndpointAddress(new Uri("http://domain.name/myservice.svc"));
MyService.MyServiceClient client = new MyService.MyServiceClient(binding, endpoint);
I am receiving the following error.
No elements matching the key 'BasicHttpBinding_IMyService' were found in the configuration element collection.
I have included my app.config below but, as you can see, I do have the binding defined.
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IMyService" >
<... removed directives for ease of reading ...>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://domain.name/MyService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyService"
contract="MyService.IMyService" name="BasicHttpBinding_IMyService" />
</client>
</system.serviceModel>
I'm sorry if this is a simple question but I haven't been able to identify the problem. I'll call it the 'late-Friday brain fog', and maybe you can call it 'easy points'?
Thanks!
The bindings in the Web.config of the WCF application and the app.config of the client application must match
When I deployed my WCF Data Services to production hosting I started to get the following error (or similar depending on which auth schemes are active):
IIS specified authentication schemes
'Basic, Anonymous', but the binding
only supports specification of exactly
one authentication scheme. Valid
authentication schemes are Digest,
Negotiate, NTLM, Basic, or Anonymous.
Change the IIS settings so that only a
single authentication scheme is used.
Apparently WCF Data Services (WCF in general?) cannot handle having more than once authentication scheme active.
OK so I am aware that I can disable all-but-one authentication scheme on the web application via IIS control panel .... via a support request!!
Is there a way to specify a single authentication scheme on a per-service level in the web.config?
I thought this might be as straight forward as making a change to <system.serviceModel> but... it turns out that WCF Data Services do not configure themselves in the web config. If you look at the DataService<> class it does not implement a [ServiceContract] hence you cannot refer to it in the <service><endpoint>...which I presume would be needed for changing its configuration via XML.
P.S. Our host is using II6, but both solutions for IIS6 & IIS7 appreciated.
Firstly it is possible to configurate Data Services on the web config file. The contract used by the DataService is called System.Data.Services.IRequestHandler.
Here is what you can do in the web config file to configurate it.
On the Service tag of the system.servicemodel element add the
<service name="{you service type name including the namespace i.e. myapplication.myservice}">
<endpoint address="" binding="webHttpBinding" contract="System.Data.Services.IRequestHandler">
</endpoint>
</service>
Once you have that there you can start configuring all manners of thing using the standard WCF configuration elements.
Secondly to enable or disabled authentication methods for a specific service in IIS you can do the following:
On the snap in for IIS right click your service file (i.e. yourservice.svc) and click properties.
Once in properties go to File Security Tab and chose the Edit button on the authentication and access control group box. after that it is just like setting up directory security in IIS.
As a last suggestion as per any trouble shooting goes it is important to enable the wcf disgnostics while you configurate it using the xml configuration, being written in WCF, Data Service logging is as per wcf is rich and very informative.
you can find out more about that on WCF Administration and Diagnostics
I hope i was able to help you with your problem
let me know how things goes.
Regards
Daniel Portella
UPDATE:
Hi Schneider
To specify the authentication scheme in the xml read below
For windows authentication as a example
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="MyBindingName" >
<security mode="Transport">
<transport clientCredentialType="Windows" />
</security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="{you service type name including the namespace i.e. myapplication.myservice}">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="MyBindingName" contract="System.Data.Services.IRequestHandler">
</endpoint>
</service>
</services>
</system.serviceModel>
</configuration>
For other types of authentication please check the MSDN library for examples
Common Scenarios for security
We have a system where the users access a web server, the web server then calls a WCF service.
We would like the call to the WCF service to be made in the security context of the windows identity of the application pool on the web server.
What is the best way to do this? Can it be done purely through configuration in the web.config file.
Thanks
Shiraz
Yes, you should be able to do this, all in config:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="WinAuth" mode="Transport">
<transport clientCredentialType="Windows" />
<bindings>
</netTcpBinding>
</bindings>
</system.serviceModel>
Of course, depending on your binding, you'd have to use a different tag under the <bindings> parent node - and of course, not all bindings support all security modes.....
In your endpoint, use the appropriate binding and then just reference this config:
<endpoint name="WCFService" address="......."
binding="netTcpBinding"
bindingConfiguration="WinAuth"
contract="......" />
That should do it! And of course, if you need message security instead of transport security, you can do that, too.
In your WCF service method, you can check to see whether or not the Windows credentials have been sent over, and what they are, by checking:
ServiceSecurityContext.Current.WindowsIdentity
This will be NULL if you don't have a Windows caller, otherwise it will show who called you.
Marc
I am developing a wcf service .
I have created two dll's one for message contracts and one for service contracts interfaces.
I share these two dll's with server and client.
I am not using AddServiceReference i am using ChannelFactory class to create proxies.
Following is the code which i am using to create client proxies:
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress endpoint = new EndpointAddress(new Uri ("http://localhost:8989/HelloService/"));
ChannelFactory<IHello> chanFac = new ChannelFactory<IHello>(binding, endpoint);
IHello clientProxy = chanFac.CreateChannel();
Now I have to create the binding and EndpointAddress in the code,what i want that this should come from app.config file , how can i do it so that i don't need to write binding and endpoint everytime in the code....
Any help is appreciated..
Use an app.config like this (when you use "Add Service Reference" from Visual Studio, VS will typically create this for you automatically - and you just need to tweak it to your needs):
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="UserNameSecurity">
<security mode="Message">
<message clientCredentialType="UserName"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8888/MyService" binding="basicHttpBinding"
bindingConfiguration="UserNameSecurity" contract="IMyService" />
<endpoint address="net.tcp://localhost:8484/MyService/Mex"
binding="mexTcpBinding"
bindingConfiguration=""
contract="IMetadataExchange" name="mexNetTcp" />
</client>
</system.serviceModel>
</configuration>
The section and its possible values and subsection are well documented in the WCF configuration.
Alternatively, in VS 2008 SP1, you can use the "WCF Service Configuration Editor" - see it in "Tools > WCF Service Configuration Editor".
It allows you to visually define and modify your client config settings. Once you've launched it from the Tools menu, after that, you can acutally even right-click on the app.config in your Solution Explorer and launch it from there (using that app.config as its basis).
Marc