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.
Related
I followed this documentation on setting up a connected wcf service in VS and things worked nicely. https://learn.microsoft.com/en-us/dotnet/core/additional-tools/wcf-web-service-reference-guide
However, now I'm struggling to figure out how to send this SOAP service my credentials (un, pwd).
Here's what the Connected Service looks like...
https://photos.app.goo.gl/hA6ABN2DNVtXW5KC8
How can I invoke and send my credentials? Then view the result?
You can call server-side methods and properties through the generated proxy class.Here is my demo:
This is a proxy class generated by WCF web service reference provider tool.We need to call services through these classes.
class Program
{
static void Main(string[] args)
{
ServiceReference1.Service1Client service1Client = new ServiceReference1.Service1Client();
System.Threading.Tasks.Task<ServiceReference1.Result> user=service1Client.GetUserDataAsync("Test");
Result rr=user.Result;
Console.WriteLine(JsonConvert.SerializeObject(rr));
Console.ReadLine();
}
}
I create a client through the proxy class and call the service through the created client.
In your code,you also need to create a client like this, and then invoke or send your credentials through the client's method.
Is there any way to configure WCF Service Reference from configuration file? I would like to configure WCF service reference with such settings as SecurityMode, Address, ReaderQuotas etc. I would also like to be able to choose between WsHttpBinding, BasicHttpBinding, BasicHttpsBinging etc (like normal configuration provided by app.config in .NET Framework).
Is there any way to achive that in .NET Core/.NET Standard?
Thank, Bartek
Certain binding, such as Wshttpbinding,Netnamedbinding is not compatible with DotNet Core framework. Consequently, we could not configure it. However, this doesn’t represent that we can’t configure Basichttpbinding, Nettcpbinding.
At present, the WCF service cannot be created by using DotNet Core without using the third-party library. Moreover, WCF client based on DotNet Core just a compatible workaround.
https://github.com/dotnet/wcf
Like the DotNet Framework project, Microsoft Corporation provides Microsoft WCF Web Service Reference Provider tool to generate a client proxy.
https://learn.microsoft.com/en-us/dotnet/core/additional-tools/wcf-web-service-reference-guide
After adding connected service, it shall generate a new namespace contains the client proxy class. Most of the client configuration located in the Reference.cs.
Also, we could manually program the code to call the WCF service.
class Program
{
static void Main(string[] args)
{
//using the automatically generated client proxy lcoated in the Reference.cs file to call the service.
//ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
//var result = client.TestAsync();
//Console.WriteLine(result.Result);
//using the Channel Factory to call the service.
Uri uri = new Uri("http://10.157.13.69:21012");
BasicHttpBinding binding = new BasicHttpBinding();
ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri));
IService service = factory.CreateChannel();
var result = service.Test();
Console.WriteLine(result);
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
string Test();
}
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory
Feel free to let me know if there is anything I can help with.
I have a WP8 app and this app needs to consume a WCF service to send/receive data from my server.
I have the service and the app working well in my developer computer, using localhost and VS2013.
Now I installed the WCF service in IIS and the service is working well!
My question is: I want a way to dinamicaly change the address of my WCF service without the need to recompile the app and deploy it!
I've found this peace of code in another thread at SO that I would like to know if it would work in any address that I change in my app dinamycally:
private MyServiceClient GetMyServiceClient(string url)
{
Uri uri = new Uri(url);
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
EndpointAddress address = new EndpointAddress(uri);
MyServiceClient client = new MyServiceClient(binding, address);
return client;
}
My app will run for now just in intranet enviroment, so I don't need a high security level to access my server, the basic httpbinding it's for now good enough.
You can look at putting your wcf configuration into a configuration file. See the post here - http://msdn.microsoft.com/en-us/library/ms733932(v=vs.110).aspx
Also take a look at this link - http://www.codeproject.com/Articles/576820/Basic-Step-by-Step-WCF-WebService
It gives an example of consuming a wcf endpoint that is defined in a config file. It uses a wpf example, but it should be easily adaptable to your scenario.
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 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"