My Quickest Start
- Designing Service Contracts
- Start simple, grow in time.
- Implement the contract
- Host the service - Console (starting simple).
- http://msdn.microsoft.com/en-us/library/ms731758.aspx
- New 'Console Application'
- See 'Example Host Code' below...
- Add Reference.... Select System.ServiceModel
- Reference 'ServiceModel'
- Start the service code
- Using
- Open
- Close
- Disable WcfSvcHost (if needed)
- "Right-click the WCF Service Project in Solution Explorer, choose Properties, and click WCF Options tab. The Start WCF Service Host when debugging another project in the same solution check box is enabled by default."
- CLIENT SIDE - Obtain the Service Contract, Bindings, and Addresses
- Enable the service
- Set the Host as the default startup program and run in without debugger (usually <CTRL>+F5).
- 'Add Service Reference...'
- Past the service address in the 'Address' field and 'Go'.
- Point the service and 'OK' =results=> a new client proxy code will be generated in your client.
- SET YOUR CLIENT APPLICATION AS THE STARTUP PROGRAM :)
- Add service usage code to your client (see 'Example Client Code' below...).
- Run and relax :-).
Code Snippet
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ServiceModel;
- using System.ServiceModel.Description;
- using MyWcfLib;
- namespace MyServer
- {
- class Program
- {
- static void Main(string[] args)
- {
- Uri baseAddress = new Uri("http://localhost:9090/hello");
- using (ServiceHost host = new ServiceHost(typeof(MyService), baseAddress))
- {
- // Enable metadata publishing.
- ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
- smb.HttpGetEnabled = true;
- smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
- host.Description.Behaviors.Add(smb);
- // Open the ServiceHost to start listening for messages. Since
- // no endpoints are explicitly configured, the runtime will create
- // one endpoint per base address for each service contract implemented
- // by the service.
- host.Open();
- Console.WriteLine("The service is ready at {0}", baseAddress);
- Console.WriteLine("Press <Enter> to stop the service.");
- Console.ReadLine();
- // Close the ServiceHost.
- host.Close();
- }
- }
- }
- }
Example Client Code
Code Snippet
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- var client = new ServiceReference1.MyServiceClient();
- Console.WriteLine(client.GenerateGuid().ToString());
- client.Close();
- Console.ReadLine();
- }
- }
- }
MSDN's 'Basic Programming Lifecycle'
Please, post comments if you find errors.
I'll fix them ASAP and others can enjoy a cleaner clode.
Hope it helps...
No comments:
Post a Comment