Excuse my ignorance if this is something basic, I am somewhat new to the whole C# console application, using a Web Service Reference (WCF).
I am creating an application that will be places as an executable on many different machines and acts as a client to a web service. however, the web service is also installed in the client's local intranet, so the endpoint will change for each location.
I see the web reference endpoint is in the app.config file when adding within Visual Studio, but is there a simple way to change the endpoint at runtime, verify the endpoint is correct and exists, and be able to use the methods?
Also, if this is possible would I still need to add the SVC as a service reference in my Solution or would I call it dynamically?
I am using C# and .Net 4
Thanks
You can put the address as an AppSetting and set it after creating your proxy class:
Proxy.Endpoint.Address = new EndpointAddress("myconfigvaluehere");
First - there's excellent book about WCF services by Juval Lowy: Programming WCF Services: Mastering WCF and the Azure AppFabric Service Bus. He explains along the book about WCF wrapper class that connect to WCF service dynamically using Endpoint, Binding and all that jazz. But you need to understand WCF background to continue working with it <- and you can do it from the book :)
Related
I am trying to migrate my code from normal wpf application to Universal Windows Platform.
Previously I have used VS 2012 for Developing my WPF application And its working fine. Now I want to use the same application for mobile also.I found UWP as a solution for it. Now I am using vs 2015 . But here in vs 2015 I am unable to find Web Reference Option I am able to see only Service Reference. Actually My situation is I am Dynamically taking Ip Address and port number from User Based on the request I am connecting to that particular services like
public const string GET_SKU_DETAILS = "http://{0}:{1}/OmniRetailerServices/SkuServices/getSkuDetails?skuID={2}";
So like this I am connecting to particular Remote servers for accessing services.
And I did this using Web References in vs 2012. But Here in vs 2015 I am not able to find Web Reference. So My Question is How can I do this in UWP application Using VS 2015.
Please Help me to solve this Issue.
Thanks In Advance.
Web references (ASMX web service clients) are considered as legacy even in classic desktop applications, such as WPF. Service references (WCF service clients) are their successor and as you already noticed, they are available in UWP apps.
Just create a service reference instead of a web reference. You should be able to create it by pointing at the same WSDL as you did to create a web reference.
You can change the endpoint URL when creating an instance of the client proxy:
// use the base URL without the method name
var url = String.Format("http://{0}:{1}/OmniRetailerServices/SkuServices", hostname, port);
var client = new SkuServicesClient(new BasicHttpBinding(), new EndpointAddress(url));
To invoke the web method, call the corresponding method on the generated proxy:
var result = await client.GetSkuDetailsAsync(argument);
Not knowing the details about your service, I tried to come up with reasonable URL value and names in my code to match the info you have provided. It should be enough to get you going even if they won't be identical in your case.
Yes you can. Just proceed as if you gonna add a new service reference (Yes I now that you need to add a web reference) then on the bottom left of the "Add Service Reference" window, click on "Advanced" option. Then, again on the bottom, you see "Compatibility" and near the "Add Web Reference" button, now got it! :)
you can't do this. you need to find another way like HttpClient.
HttpClient is the easy way to consume web services.
https://blogs.windows.com/buildingapps/2015/11/23/demystifying-httpclient-apis-in-the-universal-windows-platform/
I have a web site and would like to expose certain functionalities using WCF.
Before deciding which type of WCF project I need to use I wanted to compare the differences between WCF Class library and WCF application. I know this question has been asked many times and answered many times and the answers are usually about different hosting options each one offers but I wanted to try and see the differences, so this is what I tried:
Step 1 - In a same solution, I have created a WCF Service library project and created Client console app project, set the console app as a startup project, referenced the service library project from the client project
and in the client console app I could instantiate the service and can consume the service methods. I didn't even add a service reference to the client project.
Step 2 - In a same solution, I have created a WCF Service application project and created Client console app project, set the console app as a startup project, referenced the service application project from the client project
and in the client console app I could instantiate the service and can consume the service methods. I didn't even add a service reference to the client project.
On both steps after compiling the solutions I was able to copy the client app's exe and the service dll's to a different location and still be able to run the clients.
Based on this little excercie I am confused about the hosting part. It seems wether I use WCF Class library or WCF application type I get the same result.
This is just like using multiple projects in a solution, you reference one from another and use the methods, there must be something I am missing which highlights the differences between the two and highlights the benefits of using WCF, also in the past I remember I had to add a service reference to the clients apps in order to consume the service, why is this not the case here?
Thanks
1) Running a wcf service application allows you to provide communication into a single application, where you have a single instance of a thing you want to provide access to. Maybe this is a game, or a chat room without an external state engine or datastore. This is useful for providing diagnostic information about an application you might have written for example. I used this to provide external control for an industrial robot that I wanted to provide remote control access for.
That is to say, that you write an application, it has a function. You want to expose part of that functionality to remote applications. You do this by adding a WCF endpoint to your existing application, so your application itself is controlling the WCF hosting elements, lifecycle of the endpoint etc.
2) Running a WCF Service is for when you've got an external data store, or your service is stateless. A translation service, lookup service and web page requests fall into this category.
With a service class, you're saying here is this service, this thing that provides a function. It isn't tied to the lifecycle of another application or process and is typically hosted by IIS. IIS manages when the class is loaded and run based on the requests that come into it. These services have no internal persistence and rely on an external datastore, or are, by nature, stateless (think of a postcode lookup, or a calculator service)
It sounds like you're actually adding the projects as references, rather than connecting to them as services. That is to say, that the consuming application is actually loading the service as an assembly (in the same application/ memory space) rather than as a separate application/ service that your application then uses WCF to communicate with.
The WCF Service Application template can be used to create WCF services with a hosting website created within the project
The WCF Service Library template can be used to create WCF services that are hosted by the WCF Service Host, and these can be tested using the WCF service Test Client.
the biggest advantage of using a standalone library (apart from decoupling the logic) is that you can easily migrate your service, i.e. host it in another application or another type of application. E.g., let's say you're hosting your service using IIS - you can easily move your service to a standalone application, etc.
I know how to create a restful web service using WCF. If i create the service as a "WCF Service Library" and implement it in a solution, how do i activate it, when it is not the main project?
I am using a N-tiered architecture. The webservice should have access to some layers below it, while clients from the outside should be able to call the webservice.
What is the best way to host a service in my case ? Windows Service? IIS ? Self-hosting?
Thanks guys
Thera are various options to host your wcf service , theory behind each option is detailed here
http://msdn.microsoft.com/en-us/library/ms730158.aspx
It looks like you don't have any hosting code , in that case hosting using iis is your best option as all other options require you to have some hosting code I.e. a main entry point.
For iis you just have to create a web application project as instructed in the iis hosting section in the following article
http://www.codeproject.com/Articles/150066/Create-Host-Self-Hosting-IIS-hosting-and-Consume-W
You can do this in two ways I think:
Visual Studio can host the service for you if you want (It should have set this up automatically)
Create a separate Console project where you host your WCF Service. Then change the solution to have multiple start-up projects, so that you can start both your app and the console for WCF. More info about self hosting here
You can configure a solution to start multiple projects. Here is the MSDN link for this.
I'm migrating an old Delphi application that I wrote into C#. The application is a datalogger that exposes logged data requests via a SOAP web service interface.
The web service is contained with the delphi graphical windows application, i.e. no need to run a web server like IIS, etc I just run the application and it's up and running under the hood.
I'm looking to do the same in my c# Windows form application, I can find loads of resources on writing web services that are ultimately hosted within IIS but am struggling to find a solution for a self contained web service within my application.
Does anyone have any suggestions or can point me towards any resources on this?
The web service does not neceserily have to be SOAP, REST is fine (in fact probably prefered).
Look into WCF Services.
Hosting and Consuming WCF Services
Hosting WCF services in a Windows Forms Application
The System.Web.Hosting namespace allows you to host ASP.Net pages without using IIS within your applications. I have never used it to host web services but I found a tutorial that seems to provide a guide on doing this-
http://msdn.microsoft.com/en-us/magazine/cc163879.aspx
If you're wanting to host a service inside your application, it's possible with the System.ServiceModel.ServiceHost class. You need to learn WCF first, but that at least answers your question to get you started. If you have any further questions, leave me a comment or two and I'll update my answer to accommodate your inquiries.
I have a question. How can i invoke a web service and get the result from a C# desktop application. I am making a desktop app and I want it to be able to connect to my online ASP.net web services. How is this possible?
In Solution Explorer, right-click your project node and select Add Service Reference.
Enter the URL where your service WSDL is located. This is usually the URL of the service itself.
This generates a strongly-typed proxy class in a new Services References folder in your project.
Write code in your desktop app to instantiate the proxy class and invoke methods on it. The rest works like magic. :)
AB Kolan was also correct, but Add Web Reference uses the old-style web services framework whereas Add Service References uses the new WCF stack. Important note: It is not required that the service itself use WCF for you to use WCF on the client side. WCF on the client is typically the best choice for any service, provided you can take a dependency on .NET 3.0 and above.
Add a Web Reference to the webservice in your Desktop App project reference. Doing so would generate a Proxy for the Webservice called Reference.cs
You can access your webservice using the proxy.
This is possible the same way that you access web services from any other type of application, be it an ASP.NET page, a class library or windows service.
For an explanatory tutorial on the subject, see Accessing a Web Service from a Desktop Application.
Will get help how to create a webservice and consume that service:
http://www.c-sharpcorner.com/UploadFile/0c1bb2/consuming-web-service-in-Asp-Net-web-application/
Thanks