How to Change WCF Service endpoints? - wcf

i having more than Three web services,
In that one is master site,and Others are client sites.
In My User Interface One Text box is Available ,In that text box i need to give Destination End Point address
from that Text box Value i need call the Client Service.
for Example:
Client1 end point Service Name:
http://localhost:1524/WebServiceService.svc"
Client2 end point Service Name:
By
Rajagopalk
http://localhost:8085/WebServiceService.svc"
if i give "localhost:1524" in Text box Client1 Service will call,
if i give "localhost:8085" in Text box Client2 Service will call,

Are you hosting your WCF services in IIS ? In that case, your service address is determined by the IIS configuration and the virtual directory where your service's *.svc file exists.
So to change something on the server, you need to check and modify the IIS configuration.
To change on the client side, there's a web.config (for ASP.NET webs) or an (applicationName).exe.config where your endpoint definition should be contained - change the endpoint address there:
<client>
<endpoint name="YourEndpointName"
address="http://localhost:8085/WebServiceService.svc"
binding="......." bindingConfiguration="............."
contract="..................." />
</client>
You need to specify the complete target web service address in the address= attribute of your <endpoint> configuration element.
You can define multiple endpoints for the same service, and pick which one to use when you instantiate the client proxy:
MyServiceProxy client = new MyServiceProxy("name of endpoint configuration");
and with this, you can switch between several definitions of endpoints easily.
UPDATE: If you want to programmatically set your client address from code, you need to do the following when creating your client proxy:
// create custom endpoint address in code - based on input in the textbox
EndpointAddress epa = new EndpointAddress(new Uri(textbox.Text));
// instantiate your cilent proxy using that custom endpoint address
// instead of what is defined in the config file
MyServiceProxy client = new MyServiceProxy("name of endpoint configuration", epa);

Related

Regarding WCF endpoint address attribute

being new to wcf i often stuck to write endpoint address in config file. sometime i just could not understand what address i should write in endpoint at host end where service will be running. here is one sample endpoint entry....please have a look.
<endpoint address="net.tcp://localhost/ServiceModelSamples/Service1.svc"
binding="netTcpBinding"
contract="ServiceReference1.IService1">
</endpoint>
<endpoint address="net.tcp://localhost/ServiceModelSamples/service"
binding="netTcpBinding"
contract="ServiceReference1.IService1" />
i have couple of question on endpoint address
1) when i should write endpoint address like this way
net.tcp://localhost/ServiceModelSamples/Service1.svc
i guess when we will host our service in IIS then we need to write endpoint address like above one where we need to write our svc file name.
am i right?
2) just see the below address
address="net.tcp://localhost/ServiceModelSamples/Service1.svc"
what is ServiceModelSamples do i need to mention it?
3) suppose i develop wcf service with wcf service application template and host that service in win form apps then how our endpoint address would look like below one
address="net.tcp://localhost/ServiceModelSamples/service"
do i need to write ServiceModelSamples our project name folder or we can remove it from
address like address="net.tcp://localhost/service"
it is very important for me to know that do i need to write our project folder name like
ServiceModelSamples
4) when we host wcf service in win form apps then do i need to specify our svc file name with extension.
5) specifically guide me what address i need to write when we host our service in win form apps.
my all above question may sounds very stupid but i have really confusion about address in
endpoint. i have no there way to put question here. So please have a look at my 5 points and guide me. thanks
I think your confusion comes from the differences between IIS Hosting and custom hosting.
When you use IIS to host the service, the service behaves this way:
A base address is assigned to the service automatically depending on where the service is hosted in IIS. It uses the same address mapping that a web site uses.
The service layer needs to have .svc files to expose service instances through IIS. Those service files are just mapping files that help exposing on IIS.
So to fill the address to a service hosted in IIS from the client you need to:
Find out the address that has been assigned by IIS.
Complete that address with the Service.svc file name that you want to point to.
However when using custom hosting (through ServiceHost) the service address is totally goberned by the service configuration file:
The service can configure a base address to all its endpoint via the baseAddresses setting
All endpoints can have a full address or relative address. If relative address is used then the resulting endpoint address will be the baseAddress + endpoints relative address.
When custom hosting you have complete control over the service addresses, so you don't have any of the constraints that you must follow on IIS.

Define Endpoints for each web service and contract in wcf

I have WCf application with4 webserivce with indivdual Interface as contract. so do i need to define endpoint, serviceBehaviors for each web service in config file? to access individual web service in single or multiple web applications?
Each Webservice can be exposed using one (i.e. one endpoint for all webservices) or multiple endpoints (i.e. one for each webservice). Each endpoint will have behavior,binding etc
If you go with 1st option client will have one proxy-contract for an endpoint.
In your case you can expose all four service interface using single endpoint.
Each service must have a corresponding address, endpoint and binding definition. If you are using config files each service must be declared in a separate <service> node in the <services> collection.

WCF - Dynamically calling different web service endpoints implementing the same interface

I have a number of different applications that implement the same contract. In my main app, I would like to have one proxy. Then dynamically, given a Uri for a particular application, I would create a web service request and call that. How can that be accomplished? Thanks!
Do these steps:
generate your client-side proxy based on one service
this will generate the C#/VB.NET classes for you, as well as the app.config (or web.config if your client is a web app)
when calling the default service endpoint, you can do something like:
YourServiceClient client = new YourServiceClient();
client.CallSomeMethod();
This will use all the settings from the default service endpoint as defined in your config file
if you need to provide a different endpoint, do this:
YourServiceClient client =
new YourServiceClient("default", "http://server/YourOtherService.svc");
client.CallSomeMethod();
There is an overload for the client constructor which will take two parameters: the name of the endpoint configuration in your client config file (you need to look that up after you've added the service reference), and the endpoint URL you want to connect to (which can be different from what's stored in the config).
If all the other parameters like service contract, binding information etc. stay the same, this method should work and it should allow you to connect to any number of varying endpoints using this second constructor overload.
When you instantiate the service client, you can set the uri
ReconcileSvc.ReconcileClient client = new ReconcileClient();
client.Endpoint.Address = new System.ServiceModel.EndpointAddress(uri);
Hope it helps.

WCF proxy: Do I need to create a new and different proxy for each binding?

Let's say that I have created a WCF proxy from a WCF service (which is configured with wsHttpBinding) using Add Service (in Visual Studio 2008).
Later I want to use basicHttpBinding so I'll go and change the WCF service to use basicHttpBinding. But what about the WCF proxy? Can I just change this via Web.config or do I need to create the WCF proxy again from the WCF service via Add Service?
Thanks
It depends :-)
If you already have all the bindings in place when you do the Add Service Reference the first time around, then your client side proxy configuration will include all the bindings, and you can basically switch from using one to the other without any reconfiguration or anything. Each client endpoint (which has one specific binding) should have a name, so you can pick and choose:
MyServiceClient client = new MyServiceClient("endpointname");
However, if you add the second binding to your service after you've added the service reference to your client side code, then yes - you need to upgrade your service reference. To do so, open the Service References node in your solution explorer in the client side project, right click on that service reference you're interested in, and choose Update Service Reference from the context menu.
This will pull down any new information about additional bindings and stuff from the server side and update your client side config accordingly.
Once that's done, you should have multiple client side endpoints in your config and you can create whichever one of those is appropriate for your current needs based on the client endpoint name.

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