I am a little consused with how to acomplish this task. The question is, How can I Call a WCF Services from Multiples Hosted Servers. The WCF is the same for all the Hosted Apps. Same Contract, Same Binding Type, Etc. I am trying to call it in this way because I will host the services in multiples Servers and I need the service to do the same in all of them. I have to call it from one client. VS 2010, .Net Framework 4.0., C#.
Thanks,
It depends how you plan to create service proxy in the client application. If you want to add service reference it is enough to add it from one server and then create separate endpoint configuration for other servers - all endpoints configurations will exactly same except the address (you can do the same in code). When you call services you will create proxy instance for each server and you will pass name of the endpoint (defined in configuration) for each server like:
foreach(var endpointName in myStoredEndpointNames)
{
var proxy = new MyServiceProxy(endpointName);
proxy.CallSomeOperation();
}
Another approach is not using add service reference. In such case you must share contracts between server and client application and you can use ChannelFactory. This class is factory for client proxies which are created by calling CreateChannel. You can pass endpoint configuration name, endpoint address or binding and endpoint address when calling this method.
I use a function like this:
public static MyWcfClientType GetWcFClient(string hostName)
{
MyWcfClientType client = new MyWcfClientType();
// Build a new URI object using the given hostname
UriBuilder uriBld = new UriBuilder(client.Endpoint.Address.Uri);
uriBld.Host = hostName;
// Set a new endpoint address into the client
client.Endpoint.Address = new EndpointAddress(uriBld.ToString());
return client;
}
Of course use your own type for the "MyWcfClientType"
Related
I have the WSDL of a large enterprise web service, which includes several endpoints, out of which I only intend to use one.
I tried generating the Service Client in Visual Studio by adding a Service Reference to the project. The issue is that due to the high number of endpoints available in the service, the generated client code in Reference.cs ended up having more than 1,400,000 lines, and Visual Studio seems to have a hard time handling it (IntelliSense often hangs, sometimes the whole VS crashes), and I imagine the build times will be longer too due to this.
And out of the hundreds of endpoints, I will only use one, and won't need the rest.
Is there a way to generate the Client code from the WSDL only for a certain endpoint, either with the Service Reference GUI of Visual Studio, or with svcutil? Or if not, is there some other straightforward way to do this?
I did not find any way to generate a client for a certain endpoint from the WSDL file. But you can call the service of an endpoint individually through the channel factory.Here is the demo:
EndpointAddress address = new EndpointAddress("http://localhost:8000/GettingStarted/CalculatorService1111");
BasicHttpBinding binding =new BasicHttpBinding();
ChannelFactory<ICalculator> factory = new
ChannelFactory<ICalculator>(binding, address);
ICalculator chanel = factory.CreateChannel();
You need to call that endpoint and pass in the address of that endpoint, define the corresponding binding, and then declare the channel factory. So you can call the method in the service. By using the channel factory you can call any endpoint of a service.
I am supposed to consume a wcf service which is hosted at a server. This wcf service is not accessible to my network. Since the wcf is not accessible to me I can not add service reference and automatically create proxy classes through in visual studio. I was hoping I could write code to access this service (even though I cant actually access it on my network) and later deploy it to the server where wcf is accessible. Just like I could in case of web api.
I was referring this tutorial : https://www.aspsnippets.com/Articles/Call-Consume-WCF-Service-SVC-without-creating-Proxy-in-ASPNet.aspx
private IService GetServiceClient()
{
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress endpoint = new
EndpointAddress("http://localhost:8732/Design_Time_Addresses/WCF_NewsService/News_Service/");
IService client = ChannelFactory<IService>.CreateChannel(binding, endpoint);
return client;
}
Where do I get IService from? I am confused. All I have is a URL that I can not access. And A method string GetCustomer(string name) present in wcf which i am supposed to use.
I am consuming 2 WCF service in 1 WCF service.
using WcfService.Server1;
using WcfService.Server2;
I am calling a function that is returning endpoint name of suitable WCF service based on some logic. How to create object of WCF service based on endpoint name dynamically?
In below code I created WCF object of "Server1" WCF service but suppose method returns endpoint name "Server2Endpoint" then it would be a problem because I am referring WcfService.Server1
WcfService.Server1.MatrixCalculationClient Proxy1 = new WcfService.Server1.MatrixCalculationClient("Server1Endpoint");
I assume that both WCF services implements same interface, e.g. IWCFService.
Then I believe you will have to use either your own proxy class (which implements IWCFService independently of Server1 or Server2) or construct channel manually using ChannelFactory<IWCFService>.
In both cases you can pass name of the endpoint to the proper constructors and then everything will work if app.config file contains correct definition of these endpoints (same contract).
I am writing a basic WPF GUI to connect to a WCF service and consume an interface. So far I have connected to the test system by creating a service reference, putting in the URI for the test service I want to consume, it finds the interface and creates the proxy via service reference for me.
What I want this to do when you run the GUI app is for the user to be able to pick an environment - development, test or production and for the GUI to then connect to the appropriate WCF service depending on the environment selected.
How can I do this?
You can overwrite the Endpoint like this:
client.Endpoint.Address = new EndpointAddress(GetAddressForCurrentMode())
The other way you could to it, is to write a method, maybe an extension method, that accepts the service contract and the implementation class. Further more it either accepts a configuration name, or an endpoint:
public static TClient GetServiceClient<TClient, TContract>(string endpoint)
where TClient : ClientBase<TContract>
{
// Construct client
}
To construct the client, use one of BaseClient<T> overloads (from MSDN).
To then consume the client, just use the method above as normal:
using(var client = ServiceInterop.
GetServiceClient<MyClient, IMyContract>("http://foo.bar"))
{
// Consume client
}
I have an application where users will connect to one or more WCF services running on machines in their network. Because the address of these connections are not known at the time of installation, the application must programmatically connect to these services (i.e. I cannot use Add Service Reference). I have the connection working using the following code:
string url = "...the url...";
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress(url);
ILicenseService service = ChannelFactory<ILicenseService>.CreateChannel(binding, address);
However, some of the members of my service class return an ObservableCollection of elements. I know how to change the collection type in the Service Reference dialog box, using Advanced settings. However, I cannot figure out a way to set this value programatically so that my client knows to read the return type as an ObservableCollection instead of a List. Any clues?
It should deserialize to the type which is defined by the interface of the service class, or to the type of the property of the serialized object. Try just changing the collection type on the interface/class.
By the way, you stated that you cannot use Add service reference because the service endpoint address is not known at compile time, but that shouldn't stop you from using it. Obtain a copy of the WSDL and import it in Add service reference from local WSDL file, then when creating a service proxy specify the actual endpoint address as shown here.