A WCF service consists of:
- An interface that defines both the service and its operations.
- A class that provides the implementation of the operations.
To create a WCF service in Visual Studio:
- Add an ASP.NET Web Application project (Use the "Empty" template) or a WCF Service Application project.
- Then add a WCF Service item.
The WCF service must be hosted. The obvious choice is to host it in IIS. However, the service can be hosted in any .NET Windows application as well.
How does the host know to listen for requests? Setting in the web.config file. The following configuration indicates to the .NET framework that a WCF service is running:
<system.serviceModel> <services> <service name="ProjectName.DayOfTheWeekService"> <endpoint address="http://localhost:8000/DayOfTheWeekService.svc" binding="basicHttpBinding" name="basicHttpBinding_IDayOfTheWeekService" contract="ProjectName.IDayOfTheWeekService" /> </service> </services> </system.serviceModel>
If you wanted to host this in IIS, just publish the files to a site. If you wanted to host it in a Windows Form app, do something like this:
Private MyServiceHost As System.ServiceModel.ServiceHost Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click MyServiceHost = New System.ServiceModel.ServiceHost(GetType(ProjectName.DayOfTheWeekService)) MyServiceHost.Open() End Sub
Dim DayOfWeekProxy As New DayOfTheWeekServiceReference.DayOfTheWeekServiceClient("BasicHttpBinding_IDayOfTheWeekService") Console.WriteLine(DayOfWeekProxy.GetDayOfTheWeek(InputDate))
The client configuration is very similar to the server configuration. Essentially to connect to a service, the client must use the same configuration.
<system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IDayOfTheWeekService" /> </basicHttpBinding> </bindings> <client> <endpoint address="http://SERVERCOMPUTER:8000/DayOfTheWeekService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDayOfTheWeekService" contract="DayOfTheWeekServiceReference.IDayOfTheWeekService" name="BasicHttpBinding_IDayOfTheWeekService" /> </client> </system.serviceModel>
No comments:
Post a Comment