Using compact WCF-BasicHTTPBinding
The first of the two bindings provided out of the box with Compact WCF is the BasicHTTPBinding. This is easy to understand because it is essentially identical to making a call to a Web service. Your application sends an HTTP request to the WCF service using a SOAP-encoded message and waits for the HTTP response. In contrast with the store-and-forward e-mail transport, no unsolicited messaging is possible, so the WCF service can send messages only in a response to a request from the client. The connectivity options are essentially the same as those for a Web service
Creating a WCF Service for Compact WCF Clients The standard way to create a WCF service is to define a class annotated with the [DataContract] attribute that describes the serializable class that you use to encapsulate the data for transfer, and an interface annotated with the [ServiceContract] attribute that describes the operations exposed by the service. Then, to write the implementation of the service, you create a class that implements the service contract interface, such as the following example:
Creating a WCF Service for Compact WCF Clients
using System.Runtime.Serialization;
using System.ServiceModel;
// WCF Service class that implements the IMobileDataService service
// contract interface
public class MobileDataImpl : IMobileDataService
{
public MobileWCFObject GetMessage()
{
MobileWCFObject dataContract =
new MobileWCFObject(1, "Hello, World");
return dataContract;
}
}
An example of a class called MobileWCFObject that we use as our message payload and a custom serializer class that we use to serialize and deserialize it. You need to add a reference to System.ServiceModel and System.Xml.Serialization to your project.
use the classes on both the client and the server.
在这里可以设定message的type,可以用string[], byte[], int[], 就差不多你想传什么就传什么了
transmit data whatever you want;
其实coding of message’s define 都在这里
Definition of a Class for Transfer over WCF with Its Serializer
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
namespace NetCFDevelopersReference.WCFSample.Common
{
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(
Namespace = "http://Microsoft.ServiceModel.Samples")]
public class MobileWCFObject
{
[System.Xml.Serialization.XmlElementAttribute(Order = 0)]
public string message;
[System.Xml.Serialization.XmlElementAttribute(Order = 1)]
public int i;
}
public sealed class XmlSerializerWrapper : XmlObjectSerializer
{
XmlSerializer serializer;
string defaultNS;
Type objectType;
public XmlSerializerWrapper(Type type)
: this(type, null, null)
{ }
public XmlSerializerWrapper(Type type, string name, string ns)
{
this.objectType = type;
if (!String.IsNullOrEmpty(ns))
{
this.defaultNS = ns;
this.serializer = new XmlSerializer(type, ns);
}
else
{
this.defaultNS = "";
this.serializer = new XmlSerializer(type);
}
}
public override bool IsStartObject(XmlDictionaryReader reader)
{ throw new NotImplementedException(); }
public override object
ReadObject(XmlDictionaryReader reader, bool verifyObjectName)
{ throw new NotImplementedException(); }
public override void WriteEndObject(XmlDictionaryWriter writer)
{ throw new NotImplementedException(); }
public override void
WriteObjectContent(XmlDictionaryWriter writer, object graph)
{ throw new NotImplementedException(); }
public override void
WriteStartObject(XmlDictionaryWriter writer, object graph)
{ throw new NotImplementedException(); }
public override void
WriteObject(XmlDictionaryWriter writer, object graph)
{
this.serializer.Serialize(writer, graph);
}
public override object ReadObject(XmlDictionaryReader reader)
{
string readersNS;
readersNS =
(String.IsNullOrEmpty(reader.NamespaceURI)) ? "" : reader.NamespaceURI;
if (String.Compare(this.defaultNS, readersNS) != 0)
{
this.serializer =
new XmlSerializer(this.objectType, readersNS);
this.defaultNS = readersNS;
}
return (this.serializer.Deserialize(reader));
}
}
}
One thing that distinguishes WCF from Web Services is that-with the appropriate coding-you can host WCF services in any process, not just in IIS. For example, you can create a Windows Service or a simple Windows Forms application, add a reference to the project containing the common code from above code, and write code to programmatically build a channel, listen on a specific port, and respond to requests.
WCF Service for Compact WCF Clients
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using NetCFDevelopersReference.WCFSample.Common;
namespace NetCFDevelopersReference.WCFSample
{
public class CompactWCFServer
{
public void ReceiveCompactWCFMessage()
{
// Build the channel using the BasicHTTPBinding.
BasicHttpBinding binding = new BasicHttpBinding();
BindingParameterCollection parameters =
new BindingParameterCollection();
IChannelListener<IReplyChannel> listener =
binding.BuildChannelListener<IReplyChannel>(
new Uri("http://LocalHost:8000/MobileService"), parameters);
// Start listening for incoming requests.
listener.Open();
// Create the XMLSerializer wrapper for the object we will
// receive.
XmlSerializerWrapper wrapper =
new XmlSerializerWrapper(typeof(MobileWCFObject));
// Accept request and open response channel.
IReplyChannel channel = listener.AcceptChannel();
channel.Open(TimeSpan.MaxValue);
// Receive the message and process it.
RequestContext r = channel.ReceiveRequest(TimeSpan.MaxValue);
MobileWCFObject transferObj =
r.RequestMessage.GetBody<MobileWCFObject>(wrapper);
// Do something with it. Here, we modify its contents
// and send it back.
transferObj.message = transferObj.message + "World";
transferObj.i = transferObj.i + 1;
// Build the response message.
Message m = Message.CreateMessage(
MessageVersion.Soap11, "urn:test", transferObj, wrapper);
// Send response.
r.Reply(m, TimeSpan.MaxValue);
}
}
}
Programming the WCF Client On the device side, the first thing to realize is that in Compact WCF, unlike in WCF for the full .NET Framework, there is no support for configuration files to define to which address to make the request or the binding to use. Coupled with the requirement for custom serialization already discussed, the result requires rather more code than you may be accustomed to when programming WCF clients in the full .NET Framework.
Compact WCF Client
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using NetCFDevelopersReference.WCFSample.Common;
namespace NetCFDevelopersReference.WCFSample
{
public class CompactWCFClient
{
public void SendCompactWCFMessage()
{
// Build the message to send.
MobileWCFObject wcfMsg = new MobileWCFObject ();
wcfMsg.message = "hello";
wcfMsg.i = 5;
XmlSerializerWrapper wrapper =
new XmlSerializerWrapper(typeof(MobileWCFObject));
Message m =
Message.CreateMessage(MessageVersion.Soap11, "urn:test", wcfMsg, wrapper);
// Create the channel. In this example, we use the built-in
// text encoder and http transport to construct a channel factory.
BasicHttpBinding binding = new BasicHttpBinding();
BindingParameterCollection parameters =
new BindingParameterCollection();
IChannelFactory<IRequestChannel> channelFactory =
binding.BuildChannelFactory<IRequestChannel>(parameters);
channelFactory.Open();
// Open the channel.
IRequestChannel outChannel = channelFactory.CreateChannel(
new EndpointAddress(
new Uri("http://MyServer:8000/MobileService")));
outChannel.Open(TimeSpan.MaxValue);
// Send the message and wait for the reply.
Message reply = outChannel.Request(m, TimeSpan.MaxValue);
// Do something with the response.
MobileWCFObject to1 = reply.GetBody<MobileWCFObject>(new
XmlSerializerWrapper(typeof(MobileWCFObject)));
MessageBox.Show(to1.message + " " + to1.i.ToString());
// Tidy up.
m.Close();
reply.Close();
outChannel.Close();
channelFactory.Close();
}
}
}
以上就是compact WCF-BasicHTTPBinding 的基本应用。