Create WCF endpoint configurations in the client app, in code? - wcf

I am trying to consume a WCF web service from a .NET client application, and I think I need to be able to programmatically create endpoints, but I don't know how. I think I need to do this because, when I try to run the application, I am getting the following error:
Could not find default endpoint
element that references contract
'IEmailService' 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.
While troubleshooting this error, I created a simple windows forms application, in which I try to consume the same web service. With this test application I can connect to the web service successfully, and I get a valid response. But, I can reproduce the exact error cited above within in my test app by removing the system.serviceModel node and all of its child nodes from the application's app.config file (I might not have to remove ALL of that section, I'm not sure). So, my first thought was that I need to add that section to the app.config file for the real app, and everything should be fine. Unfortunately, for ridiculous reasons that I won't get into here, that is not an option. So, I am left with having to generate this information in code, inside the client app.
I am hoping someone here can help me work through this, or can point me toward a good resource for this sort of problem.
Is it possible to create endpoint configurations in the client app, in code?

By default, when you do an Add Service Reference operation, the WCF runtime will generate the client-side proxy for you.
The simplest way to use it is to instantiate the client proxy with a constructor that takes no parameters, and just grab the info from the app.config:
YourServiceClient proxy = new YourServiceClient();
This requires the config file to have a <client> entry with your service contract - if not, you'll get the error you have.
But the client side proxy class generated by the WCF runtime also has additional constructors - one takes an endpoint address and a binding, for instance:
BasicHttpBinding binding = new BasicHttpBinding(SecurityMode.None);
EndpointAddress epa = new EndpointAddress("http://localhost:8282/basic");
YourServiceClient proxy = new YourServiceClient(binding, epa);
With this setup, no config file at all is needed - you're defining everything in code. Of course, you can also set just about any other properties of your binding and/or endpoint here in code.

An east way to consume a WCF service if you have a reference to the assembly which defines the interface, is using the System.ServiceModel.ChannelFactory class.
For example, if you would like to use BasicHttpBinding:
var emailService = ChannelFactory<IEmailService>.CreateChannel(new BasicHttpBinding(), new EndpointAddress(new Uri("http://some-uri-here.com/));
If you don't have a reference to the service assembly, then you can use one of the overloaded constructors on the generated proxy class to specify binding settings.

Related

Does WCF Add Service Reference require something configured on the service to generate the app config?

We have an existing wcf service, and I created a new project. I want to use it. I hit add service reference, pop in the URL, press OK, and it adds it as a service reference but there is no config generated.
I also tried svcutil.exe /language:cs /out:GeneratedProxy.cs /config:app.config [url] but no config is generated, only the proxy cs.
I'm using VS 2013 / .NET 4.0
My question is, is this a sign that the SVC itself has some missing data that is required to build the contracts, or is the problem with adding the service reference?
For the record I have tried unchecking the reuse types option which some questions on here have reported as fixing the problem.
Bonus question, do you think if I can't get this working that manually adding some generic default bindings and endpoint code to the web config will work?
First, the reason that why the Adding service reference generates nothing is that the WCF service is rest style service. By default, the proxy-based invocation of rest style WCF services is complex.
https://en.wikipedia.org/wiki/Representational_state_transfer
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/wcf-web-http-programming-model
Calling the WCF rest style service with the client proxy is uncommon. Generally, we construct an Http request by using an HTTP client library to call the service, such as HttpClient, WebClient.
How to fix "ERR_ABORTED 400 (Bad Request)" error with Jquery call to C# WCF service?
Besides, calling the WCF rest style service with the client proxy is feasible. Please refer to my previous link.
WCF: There was no endpoint listening at, that could accept the message
Feel free to let me know if there is anything I can help with.

Quick and easy implementation of a WCF web service, given wsdl?

I'm writing a client to access a SOAP webservice, that will be hosted by a third party. I have the WSDL and XSD that define the interface and the data.
I've had no problem in creating a service reference from the WSDL, but I'm having a problem in building a simple web service that implements it, that I can use to test against. (The third party's service isn't ready, yet, but even were it running, I'd still like to do my initial testing against my own test server, not against theirs.)
I've browsed around, and apparently I can use svcutil to generate an interface for the service:
svcutil.exe thewsdl.wsdl thexsd.xsd /language:c# /out:ITestService.cs
This generates a file containing the service interface definition. But now what?
I figured the easiest way to go would be to build a self-hosted service, so I created a new console app, and in it I implemented a class derived from the service interface definition, and fired it up with a ServiceHost.
It runs, and while it was running I was able to create a Service Reference in my client app. But when I try to call it, from the client app, I get an error:
The provided URI scheme 'http' is invalid; expected 'https'.
What is the easiest way to get around this? Is there a simple way to simply turn off authentication and authorization, and simply allow unrestricted access?
EDITED:
I'm adding a bounty to this, as the original question seems to have attracted no attention.
But let's get to the crux. I am trying to write a client against a customer's SOAP service. As a part of the development, I want to create my own test WCF service, that implements the same WSDL.
So I have a downloaded .wsdl file, and an associated .xsd file, and with them I want to create a service that I can test against, with VS2010's debugger.
It's not important to me whether this service runs standalone, or within IIS, or that it be production stable. All I want is a service that accepts the requests that the customer's site would accept, and return the responses to my client that I need it to return, in order to test my handling of them.
How do I get there? I've tried adding a WCF Service Library, and then using svcutil.exe within it to add my new service, but it doesn't seem to populate the app.config with the server-side boilerplate, and my attempts to reconstruct it haven't worked.
Since you want a full fledged service to call instead of mocking it.
Follow these steps:
Create new "WCF Service Application" project
Copy wsdl and xsd into project
select your wsdl file and look in the properties section and copy location from full path
Right click on the project in solution explorer and select "Add Service Reference..."
For the service address, paste the location of your wsdl that was copied in previous step and hit go. It should show the operations you are expecting for the service.
hit ok
It should generate all the objects for you including the interface and config file (although at this point is client side in the config- we will have to switch this to be the service)
Now you should add the service config section in the system.serviceModel section. Since I don't know the specifics of your wsdl what you should do is create the services node inside the system.serviceModel section and copy the endpoint node from the client node generated. For example below of services node, you can blank out the address for now:
<system.serviceModel>
<services>
<service name="YourService">
<endpoint address=""
binding="basicHttpBinding" bindingConfiguration="WeatherSoap"
contract="ServiceReference1.WeatherSoap" name="WeatherSoap" />
</service>
delete the client node in the config
In the service, it is implementing a different interface when it generated the project so you will want to replace the interface implemented with the one listed in the contract attribute in the endpoint above. Then implement its members and it should explode out the operations available. You can fill in whatever you want the service operations to return.
depending on what the wsdl has in it, we may need to do a few more things to allow the necessary bindings to run - like setting up for wsHttpbinding, netTCPbinding, etc.
I've used Moq to handle this. Basically in the unit tests you specify the interface (this would have been generated for you with adding the service reference or using svcutil) and what you want it to return if you call it.
example setup below:
var mock = new Mock<IFoo>();
mock.Setup(foo => foo.DoSomething("ping")).Returns(true);
So then when you want to moq out your service call
var myObject = new IFoo;
var resp = myObject.DoSomething("whateverwillbeoverriddenbyping");
and resp will be true.
There are other options than using Moq. The options all involve taking the interface and injecting a different version of it. For example, you could also do a constructor injection mock, by passing in the interface to your class constructor.

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.

How to create Endpoints from app.config file in WCF?

I have a service which has one endpoint , I have defined this endpoint in app.config file.
I want to know how can I create endpoints if I have app.config in program.
Please give me an idea.
Do you have a generated proxy for your service? If so, just use the proxy client!
MyServiceClient proxy = new MyServiceClient();
Optionally, you can pass in a name for the configuration to use:
MyServiceClient proxy = new MyServiceClient("MyConfigName");
No need to do anything fancy.
If you haven't created a proxy (using "Add Service Reference" in Visual Studio or svcutil.exe on the command line), you'll need to add a reference to your assembly containing the service and data contracts, and then use
ChannelFactory<IMyService> factory = new ChannelFactory<IMyService>();
IMyService proxy = factory.CreateChannel( );
Again, for creating the channel factory, you can pass in a name of a configuration section, if you have multiple, to specify which one to use.
Also, to clarify - a client can only ever have one endpoint at any given time. The service might have multiple - but the client needs to make up its mind and connect to exactly one of those - you cannot have multiple endpoints in a client (as the title of your questions appears to imply).
Marc
If you are using Visual Studio use the WCF Service Configuration Editor (found under tools). Use this to open your config file or hosted service and then you can create your endpoints there. Any new endpoint configuration info will be saved into your app.config/web.config as appropriate

How to connect to a WCF Service with IronPython

Has anyone done this? I've tried generating a c# proxy class and connecting through it, but I cannot figure out how to get IronPython to use the generated app.config file that defines the endpoint. It tries to connect, but I just get an error about no default endpoint. I would ideally like to make the connection using only IronPython code and not use the proxy class, if possible. The binding for the service I am trying to connect to is a NetTcpBinding if that makes any difference.
See my blog post. There are IronPython WCF service and client examples.
To use app.config, you probably must copy it to ipy.exe and rename it to ipy.exe.config but I have not tried it so I don't know whether it works or not.
Is your WCF service interface available in a shared assembly? If so, you could look at using the ChannelFactory to create your client proxy dynamically (instead of using the generated C# proxy). With that method you can supply all the details of the endpoint when you create the ChannelFactory and you won't require any configuration in your .config file.