Вы находитесь на странице: 1из 56

[DataContract]

public class Invoice { [DataMember] public string CustomerId; [DataMember] public string InvoiceDate; [DataMember] public double Amount; ... }

[ServiceContract] public interface IInvoiceService { [OperationContract] void SubmitInvoice(Invoice invoice); } public class InvoiceService : IInvoiceService { public void SubmitInvoice(Invoice invoice) { ... // implementation omitted } }


<configuration> <system.serviceModel> <services> <service name="InvoiceService"> <endpoint address="http://server/invoiceservice" binding="webHttpBinding" contract="IInvoiceService"/> <endpoint address="net.msmq://server/invoicequeue" binding="netMsmqBinding" contract="IInvoiceService"/> <endpoint address="net.tcp://server:8081/invoiceservice" binding="netTcpBinding" contract="IInvoiceService"/> </service> </services> </system.serviceModel> </configuration>

ChannelFactory<IInvoiceService> cf = new ChannelFactory<IInvoiceService>(endpoint); ...

ChannelFactory<IInvoiceService> factory = new ChannelFactory<IInvoiceService>( new BasicHttpBinding(), new EndpointAddress("http://server/invoiceservice")); ...

ChannelFactory<IInvoiceService> factory = new ChannelFactory<IInvoiceService>("httpEndpoint"); ...

<configuration> <system.serviceModel> <client> <endpoint name="httpEndpoint" address="http://server/invoiceservice" binding="basicHttpBinding" contract="IInvoiceService"/> <endpoint name="tcpEndpoint" address="net.tcp://server:8081/invoiceservice" binding="netTcpBinding" contract="IInvoiceService"/> </client> </system.serviceModel> </configuration>

ChannelFactory<IInvoiceService> factory = new ChannelFactory<IInvoiceService>("tcpEndpoint"); IInvoiceService channel = factory.CreateChannel(); channel.SubmitInvoice(invoice); ((IClientChannel)channel).Close();

public interface IInvoiceServiceChannel : IInvoiceService, System.ServiceModel.IClientChannel { } ChannelFactory<IInvoiceServiceChannel> factory = new ChannelFactory<IInvoiceServiceChannel>("tcpEndpoint"); IInvoiceServiceChannel channel = factory.CreateChannel(); channel.SubmitInvoice(invoice); channel.Close();

public partial class InvoiceServiceClient : ClientBase<IInvoiceService>, IInvoiceService { public InvoiceServiceClient() { } public InvoiceServiceClient(string endpointName) : base(endpointName) { } ... // you're service contract methods will be here

... InvoiceServiceClient client = new InvoiceServiceClient("httpEndpoint"); Invoice invoice = new Invoice(); invoice.CustomerName = "Acme, Inc"; invoice.Amount = 100.00; invoice.InvoiceDate = DateTime.Now; client.SubmitInvoice(invoice); client.Close(); ...

<configuration> <system.serviceModel> <client> <endpoint name="httpEndpoint" address="http://server/invoiceservice" binding="basicHttpBinding" bindingConfiguration="MyConfiguration" contract="InvoiceServiceReference.IInvoiceService" /> </client> <bindings> <basicHttpBinding> <binding name="MyConfiguration" sendTimeout="00:05:00"> <security mode="Transport"> <transport clientCredentialType="Basic"/> </security> </binding> </basicHttpBinding> </bindings> </system.serviceModel> </configuration>

<configuration> <system.serviceModel> <client> <endpoint name="httpEndpoint" address="http://server/invoiceservice" binding="basicHttpBinding" behaviorConfiguration="viaBehavior" contract="InvoiceServiceReference.IInvoiceService" /> </client> <behaviors> <endpointBehaviors> <behavior name="viaBehavior"> <clientVia viaUri="http://router/invoiceservice"/> </behavior> </endpointBehaviors> </behaviors> ...

InvoiceServiceClient client = new InvoiceServiceClient("httpEndpoint"); Invoice invoice = ... // create invoice try { client.SubmitInvoice(invoice); client.Close(); } catch (FaultException fe) { Console.WriteLine(fe); client.Abort(); } catch (CommunicationException ce) { Console.WriteLine(ce); client.Abort(); } catch (TimeoutException te) { Console.WriteLine(te); client.Abort(); } ...

public abstract class Message : IDisposable { // numerous overloads for creating messages public static Message CreateMessage(...); // reads the body as XML public XmlDictionaryReader GetReaderAtBodyContents(); // deserializes the body into a .NET object public T GetBody<T>(XmlObjectSerializer serializer); // numerous methods/overloads for writing messages public void WriteMessage(XmlDictionaryWriter writer); ... }

[DataContract]
public class Invoice { [DataMember] public string CustomerId; [DataMember] private DateTime InvoiceDate; public string SomePrivateData; ... }

[ServiceContract] public interface IInvoiceService { [OperationContract(IsOneWay=true)] void SubmitInvoice(Invoice invoice); [OperationContract] InvoiceStatus GetStatus(Invoice invoice); void CancelInvoice(Invoice invoice); }

[ServiceBehavior( InstanceContextMode=InstanceContextMode.Single, ConcurrencyMode=ConcurrencyMode.Multiple)] public class InvoiceService : IInvoiceService { [OperationBehavior( Impersonation=ImpersonationOption.Required)] public void SubmitInvoice(Invoice invoice) { ... // implementation omitted } ... }

class Program { static void Main(string[] args) { ServiceHost host = new ServiceHost(typeof(InvoiceService)); ... // configure the host before opening

try { host.Open(); Console.ReadLine(); host.Close(); } catch (Exception e) { Console.WriteLine(e); host.Abort(); } } }

<%@ ServiceHost Service="InvoiceService" %>

<configuration> <system.serviceModel> <!-- this is where you configure your WCF applications --> </system.serviceModel> </configuration>

... host.AddServiceEndpoint( typeof(IInvoiceService), new BasicHttpBinding(), "http://server/invoiceservice"); host.AddServiceEndpoint( typeof(IInvoiceService), new NetTcpBinding(), "net.tcp://server:8081/invoiceservice");

host.Open(); ...

<configuration> <system.serviceModel> <services> <service name="InvoiceService"> <endpoint address="http://server/invoiceservice" binding="basicHttpBinding" contract="IInvoiceService"/> <endpoint address="net.tcp://server:8081/invoiceservice" binding="netTcpBinding" contract="IInvoiceService"/> </service> </services> </system.serviceModel> </configuration>

BasicHttpBinding b = new BasicHttpBinding(); b.Security.Mode = BasicHttpSecurityMode.Transport; b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

host.AddServiceEndpoint( typeof(IInvoiceService), b, "http://server/invoiceservice"); ...

<configuration> <system.serviceModel> <services> <service name="InvoiceService"> <endpoint address="https://server/invoiceservice" binding="basicHttpBinding" bindingConfiguration="MyBindingConfiguration" contract="IInvoiceService"/> ... </service> </services> <bindings> <basicHttpBinding> <binding name="MyBindingConfiguration"> <security mode="Transport"> <transport clientCredentialType="Basic" /> </security> </binding> </basicHttpBinding> </bindings> </system.serviceModel> </configuration>

... ServiceHost host = new ServiceHost(typeof(InvoiceService)); ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; host.Description.Behaviors.Add(smb); host.Open(); ...

<configuration> <system.serviceModel> <services> <service name="InvoiceService" behaviorConfiguration="MetadataBehavior"> ... </service> </services> <behaviors> <serviceBehaviors> <behavior name="MetadataBehavior"> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>

<configuration> <system.serviceModel> <services> <service name="InvoiceService" behaviorConfiguration="MetadataBehavior"> <endpoint address="net.tcp://server:80801/invoiceservice/mex" binding="mexTcpBinding" contract="IMetadataExchange"/> ...

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <s:Fault> <faultcode xmlns:a="..." xmlns="">a:InternalServiceFault</faultcode> <faultstring xml:lang="en-US" xmlns="">The server was unable to process the request due to an internal error...</faultstring> </s:Fault> </s:Body> </s:Envelope>

<configuration> <system.serviceModel> <behaviors> <behavior name="Default"> <serviceDebug includeExceptionDetailInFaults="true" /> </behaviors> </behaviors> </system.serviceModel> </configuration>

... public void DoSomething(string input) { ... throw new FaultException("Something bad happened"); }

getUser() addUser() removeUser() ... getLocation() addLocation() ...


User { } Location { }

<user> <name>Jane User</name> <gender>female</gender> <location href="http://example.org/locations/us/ny/nyc" >New York City, NY, US</location> </user>

... ServiceHost host = new ServiceHost(typeof(EvalService), new Uri("http://localhost:8080/evals")); host.AddServiceEndpoint(typeof(IEvals), new WebHttpBinding(), ""); host.Description.Endpoints[0].Behaviors.Add( new WebHttpBehavior()); host.Open(); // service is up and running Console.ReadLine(); // hold process open ...

... WebServiceHost host = new WebServiceHost( typeof(EvalService), new Uri("http://localhost:8080/evals")); host.Open(); // service is up and running Console.ReadLine(); // hold process open ...

[ServiceContract] public interface IEvalService { [WebGet(UriTemplate="evals?name={name}&score={score}")] [OperationContract] List<Eval> GetEvals(string name, int score); ...

[ServiceContract] public interface IEvals { [WebInvoke(UriTemplate ="/evals?name={name}",Method="PUT")] [OperationContract] void SubmitEval(string name, Eval eval /* body */); ...

/services/evals?name={name}&detailed={detailed}

[ServiceKnownType(typeof(Atom10FeedFormatter))] [ServiceKnownType(typeof(Rss20FeedFormatter))] [ServiceContract] public interface IEvalService { [WebGet(UriTemplate = "evalsfeed")] [OperationContract] SyndicationFeedFormatter GetEvalsFeed(); ... }

public class EvalService : IEvalService { public SyndicationFeedFormatter GetEvalsFeed() {

List<Eval> evals = this.GetEvals(); SyndicationFeed feed = CreateSyndicationFeed(evals);


// figure out what format the client wants WebOperationContext ctx = WebOperationContext.Current; string format = ctx.IncomingRequest.UriTemplateMatch.QueryParameters["format"]; // return the if (format != return new else return new } ... right type of formatted feed null && format.Equals("atom")) Atom10FeedFormatter(feed);

Rss20FeedFormatter(feed);

[ServiceContract] public interface IEvals { [WebGet(UriTemplate = "/evals?name={nameFilter}", ResponseFormat = WebMessageFormat.Json)] [OperationContract] List<Eval> GetCurrentEvals(string nameFilter); ...

<%@ ServiceHost Language="C#" Service="EvalService" Factory= "System.ServiceModel.Activation.WebScriptServiceHostFactory" %>

Вам также может понравиться