First thing you have to decide about the interface. This is called service contract. All your classes appear for this service must implement this interface. First build a class library which will include your entire service contract. Add reference to the service model to this library. At the definition add attribute ServiceContract to you interface, OperationContract to all your methods, DataContract to any custom data type if you want them to carry via WCF and DataMemeber to all of its child data items. Here is an example of service contract.
[ServiceContract]
public interface IDiskService
{
[OperationContract]
string GetRoot();
[OperationContract]
bool CreateFile(string strhandle);
[OperationContract]
bool DeleteFile(string strHandle);
[OperationContract]
byte[] ReadFile(string strHandle);
[OperationContract]
bool WriteFile(string strHandle, byte[] data);
[OperationContract]
bool RenameFile(string strHandle, string strNewName);
[OperationContract]
string CopyFile(string strHandle, string strNewName);
[OperationContract]
FileSystemData[] Contents(string directoryHandle);
}
[DataContract]
public struct FileSystemData
{
[DataMember]
public string FileName;
[DataMember]
public int FileType;
}
This is an example of exposing a file system with WCF. Here IDiskService is our service contract. And all of its methods are operation contract. Here we use a custom data structure so we use DataContract and all of their member variables are DataMember. Microsoft claimed all the Serializable data structure can be used as input or output of the service contract but I try to use DataSet and failed.
Once you complete this step you have complete service contract. Now you have to build service provider. That is this is your service contract but there must be some one who will provide the service. And generally this is a class inherited from this interface. The service provider must have all this OperationContract that is it must have all these methods with same signature.
Now build that class and place that in any library or project. This is most simple part with respect to WCF. All you business logic belongs to it. Let I build “DiskServiceClass” for IDiskService.
And the last step is building a service host application which can host your service. Just take a console application or Windows Service application or any other type of application and add reference to servicemodel.dll. Now add an app.config file to your application. This is used to configure your service host. An example of app.config is showed below with necessary comments.
<configuration>
<system.serviceModel>
<services>
<service name = "ServiceImplementor.DiskServiceClass" behaviorConfiguration="metadataSupport">
// this is used to identify the provider class here the name is the name of my provider class. And behaviorConfiguration="metadataSupport" describe that this service will support metadata so you can get wsdl from this service.
<host>
<baseAddresses>
<add baseAddress ="http://localhost:8000/Services/DiskService" />
</baseAddresses>
// this is the address of the service to locate it.
</host>
<endpoint address = "" binding="wsHttpBinding" contract="Services.IDiskService" />
// this is used to identify the protocol stuck, as we know WCF is designed to run over multiple protocol stuck, so here we have to select the protocol stuck, which must match with address like if you want to use tcp then address must net.tcp etc.
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="metadataSupport">
<serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
That’s it; you are almost at the edge. And now you have to write a simple code, in main function.
ServiceHost host = new ServiceHost(typeof(DesktopServiceLibrary.DiskService));
host.Open();
Console.WriteLine("Service Host is ready to service");
Console.ReadKey();
host.Close();
That’s all; now your service is ready to go. Any one can get service from your application. This is all for today. I will show customization and flexibility in my future articles. Just have fun with WCF.
Oi, achei teu blog pelo google tá bem interessante gostei desse post. Quando der dá uma passada pelo meu blog, é sobre camisetas personalizadas, mostra passo a passo como criar uma camiseta personalizada bem maneira. Até mais.
ReplyDelete