Configure WCF Service address in Xamarion iOS - wcf

In my Xamario.iOS application which I'm doing in VisualStudio, I'm consuming WCF services by adding the ServiceReferences (Add ServiceReference) and I get my services added in ServiceReference folder as below:
LoginService (This is of Login.svc)
RegisterService (This is of Register.svc)
my code looks like
LoginRequest req = new LoginRequest()
req.username = "test"
req.password = "test"
LoginResponse res = LoginService.Authenticate(req)
and I get my response which is fine.
But I would like to configure the WCF service's address dynamically, so that in future I can change my service URL just in one place rather than updating all the services under ServiceReference folder.
Right now, I don't find any config file which has the configuration details for the WCF services added, in my Xamarin.iOS project.
If I create the Service Client in code behind, I can get the client for each service, but in such case how can I have my LoginRequest and LoginResponse type?

Your client class should have a constructor overload that allows you to specify the address of the endpoint.

Related

How to prevent WCF proxy from following redirect?

I'm using basicHttpBinding for my WCF service and have a message inspector that sets response code to Redirect under certain circumstances. I find that the WCF proxy (generated by svcutil) automatically tries to follow the redirect. How do I prevent this from happening?
Thanks,
Priya
Can you reference the service contract assembly from your client application? If so you can get rid of the generated service reference and just spin up a proxy at runtime using ChannelFactory.
For example:
// Create service proxy on the fly
var factory = new ChannelFactory<IMyServiceContract>("NameOfMyClientEndpointInConfigFile");
var proxy = factory.CreateChannel();
// Create data contract
var requestDataContract = new MyRequestType();
// Call service operation.
MyResponseType responseDataContract = proxy.MyServiceOperation(requestDataContract);
In the above example, IMyServiceContract is your service contract, and MyRequestType and MyResponseType are your data contracts, which you can use by referencing the assembly which the service also references (which defines these types).
what have you tried to achieve with redirect ? you will an handle this cases with some message interceptors on client side:
http://msdn.microsoft.com/en-us/library/ms733786(v=vs.90).aspx

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.

Referencing WCF Services without mex binding

I was wondering how a client project in Visual Studio could reference a WCF service that doesn't have a mex binding. Whenever I remove the default mex binding in any sample WCF service, the client apps cannot find the service and in the auto-generated comments, it's recommended that in production environment, mex binding should be removed. Then how are the client apps supposed to find the service and reference it?
If you have access to the assemblies which contain the types which define the service contract, operations, and data contracts, then you can just create a proxy on the fly using ChannelFactory. In this instance you would not need to retrieve any service metadata as you already have access to all the information you need to call the service.
For example
// Create service proxy on the fly
var factory = new ChannelFactory<IMyServiceContract>("NameOfMyClientEndpointInConfigFile");
var proxy = factory.CreateChannel();
// Create data contract
var requestDataContract = new MyDataContract();
// Call service operation.
var responseDataContract = proxy.MyServiceOperation(requestDataContract);
It also helps if you have access to the service-side config file so you can copy the endpoint details out of there into your client config.
The mex endpoint is a necessary part of WCF SOAP services. It is what enables client toolkits to pull down the WSDL and auto-generate proxy classes. As you point out, without it, clients have no way to get the information to consume the service. If you want clients to be able to consume and find your service, you should leave it available when your service is in production.

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.

accessing WCF service through URL

I have a WCF service ( Let's say WCFService1 ) is deployed on two remote machines. Since the same service is deployed on two different machines they have common interface and common methods exposed.
WCFService1 is deployed on Machine1 and Machine2.
To consume WCF service from client machine, I have created a client app:
I have added a design time reference of WCF service (WCFService1 )( with the help of URL http://11.12.25.23/WCFService/Service1.svc).
Now I can invoke the methods exposed in the service. Up until now its fine...
Now my question is If I have to update client at run time with same service hosted in different machine with different URL ( Let's say http://12.12.24.24/WCFService/Service1.svc), How can I do that?
At present I am doing this:
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://12.12.24.24/WCFService/Service1.svc");
MyServiceClient serviceClient = new MyServiceClient(binding, address);
but whenever I use to invoke the method exposed in the service I got binding mis match error.
Have you tried invoking your client first?
eg:
MyWCFClient client = new MyWCFClient();
client.EndPoint.Address = new EndpointAddress("http://somewhere:888/here.svc");
I'd suspect, that if you look in your web.config file on Machine1, you'll see that the binding there is WSHttpBinding (or something different than BasicHttpBinding). If you change it to BasicHttpBinding (assuming that is what you really want), you'll remove this error.
How is your service configured? Show us your server-side and client-side config!
Binding mismatch means you're either not using the same binding, or some vital parameter on the binding is different - there must be something configured wrong - so show us the config!
Marc