1) ServiceContract & Implementation
you need a contract (=Interface with [ServiceContract] Attribute) like[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
which contains Methods attributed with [OperationContract]
implement the [ServiceContract] Interface:
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
assume its in namespace WcfServiceLib for hosting below
2) Host the ServiceContract
2a) with IIS
you need a .svc file with one single line:
<%@ServiceHost Service="WcfServiceLib.Service1"%>
and configure the service (provide metadata) within the web.config:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<service name="WcfServiceLib.Service1" behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="" binding="wsHttpBinding"
contract="WcfServiceLib.IService1"/>
<endpoint contract="IMetadataExchange"
binding="mexHttpBinding" address="mex"/>
</service>
</services>
</system.serviceModel>
2b) With .net app:
static void Main(string[] args)
{
Type serviceType = typeof(MyWCFServices.HelloWorldService);
string httpBaseAddress = ConfigurationManager.AppSettings["HTTPBaseAddress"];
Uri[] baseAddress = new Uri[] { new Uri(httpBaseAddress) };
ServiceHost host = new ServiceHost(serviceType, baseAddress);
host.Open();
Console.WriteLine("HelloWorldService is now running. at: " +host.BaseAddresses.First());
Console.WriteLine("Press any key to stop it ...");
Console.ReadKey();
host.Close();
}
No comments:
Post a Comment