WCF: creare serviciu, configurare, gazduire pe serverul IIS, creare client




Voi prezenta cum se poate crea un serviciu Windows Communication Foundation (Indigo), pornind de la crearea unei solutii care sa continta, serviciul in sine, o librarie de creare de obiecte business, precum si directorul virtual care va gazdui serviciul in IIS.


Pentru a folosi acestea e nevoie de .Net 3.0. Mai multe despre cum se instaleaza aici




  1. Pregatirea solutiei: Visual Studio 2005



    • se creaza o solutie de tip librarie, numita IndigoService

    • in solutie se adauga o doua librarii numite BusinessLogic si BusinessObjects

    • in directorul radacina al solutiei se creaza un director IISHosting si in acest director un alt director bin

    • in solutie se adauga un proiect de tip ConsoleApplication numit ServiceClient




  2. Adaugarea claselor


    in libraria BusinessObjects



    • se adauga referinta la System.Runtime.Serialization

    • se creaza clasa Customer


    Customer.cs



    using System.Runtime.Serialization;

    namespace BusinessObjects
    {
    [DataContract]
    public class Customer
    {
    [DataMember]public int ID;
    [DataMember]public string Name;
    }
    }


    • se compileaza libraria si se obtine dll-ul BusinessObjects.dll


    in libraria BusinessLayer



    • se adauga referinta la BusinessObjects.dll

    • se creaza clasa BLL


    BLL.cs



    using BusinessObjects;

    namespace BusinessLayer
    {
    public static class BLL
    {
    public static Customer GetCustomer ( int id )
    {
    // presupunem ca vom instantia un Customer cu informatie dintr-o baza de date
    Customer c = new Customer();
    c.Name = "Adrian Iftode";
    c.ID = id;
    return c;
    }
    }
    }


    • se compileaza libraria si se obtine dll-ul BusinessLayer.dll


    in libraria IndigoService



    • se adauga referinta la System.ServiceModel

    • se adauga referinta la BusinessLayer.dll

    • se adauga referinta la BusinessObjects.dll

    • se adauga doua clase in fisiere separate: o interfata numita IServiceIntentions si o clasa care implementeaza interfata ServiceImplementation

    • se adauga un fisier de configurare Web.config

    • se adauga un fisier de configurare pentru IIS IISHostingConfigFile.svc (se alege un Text File a carui extensie se redenumeste .svc)


    IServiceIntentions.cs



    using System.ServiceModel;
    using BusinessObjects;

    namespace IndigoService
    {
    [ServiceContract]
    public interface IServiceIntentions
    {
    [OperationContract]
    Customer GetCustomer(int CustomerID);
    }
    }

    ServiceImplementation.cs




    using System.ServiceModel.Activation;
    using BusinessObjects;
    using BusinessLayer;

    namespace IndigoService
    {
    [AspNetCompatibilityRequirements ( RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed )]
    public class ServiceImplementation : IServiceIntentions
    {
    public Customer GetCustomer ( int CustomerID )
    {
    return BLL.GetCustomer(CustomerID);
    }
    }
    }

    WebConfig.cs



    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <system.serviceModel>
    <services>
    <service
    behaviorConfiguration="MEX"
    name="ServiciulIndigo.ServiceImplementation"
    >
    <endpoint address="http://localhost/IndigoService/IISHostingConfigFile.svc"
    binding="basicHttpBinding"
    bindingName="NewBinding0"
    contract="IndigoService.IServiceIntentions"
    >
    </endpoint>
    </service>
    </services>
    <bindings>
    <basicHttpBinding>
    <binding name="NewBinding0" />
    </basicHttpBinding>
    </bindings>
    <behaviors>
    <serviceBehaviors>
    <behavior name="MEX">
    <!-- pentru a putea vedea proprietatile serviciului MetadataExchange-->
    <serviceMetadata httpGetEnabled="true"/>
    <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
    </serviceBehaviors>
    </behaviors>
    </system.serviceModel>
    </configuration>

    IISHostinConfigFile.svc



    <%@ ServiceHost
    Language = "C#"
    Debug = "true"
    Service = "IndigoService.ServiceImplementation"
    %>


    • se compileaza libraria si se obtine dll-ul IndigoService.dll




  3. Gazduirea in IIS



  4. La fiecare librarie se adauga la evenimentul PostBuild( Properties\BuildEvents) urmatoarea actiune:


    xcopy $(TargetPath) $(SolutionDir)IISHosting\bin /R /Y


    La libraria IndigoService la acelasi eveniment se mai adauga urmatoarele linii:


    xcopy $(ProjectDir)Web.config $(SolutionDir)IISHosting /R /Y


    xcopy $(ProjectDir)IISHostingConfigFile.svc $(SolutionDir)IISHosting /R /Y


    Se compileaza si se rezolva fiecare eroare :)


    Se verifica in directorul IISHosting\bin daca apar cele trei librarii si daca in directorul IISHosting apar fisierele Web.config si IISHostingConfigFile.svc


    Crearea directorului virtual


    • Se creeaza un nou director virtual pe serverul IIS: Windows+R\inetmgr.exe si se deschide IIS

    • clik dreapta pe WebSites\Default Web Site\ si New\VirtualDirectory\

    • Next.. apoi se da un nume .. se pune calea directorului IISHosting

    • Next.. se bifeaza Read, Run scripts, Browse

    • Finish


    Se selecteaza directorul creat, click dreapta, Browse. Se da click pe linkul IISHostingConfigFile.svc


    Daca totul e ok si nu apare nici un mesaj de eroare, in pagina ar trebui sa se vada cum se poate crea un client pentru serviciul WCF precum si cum se genereaza proxiul pentru serviciu, folosind tool-ul svcutil




    svcutil.exe http://localhost/IndigoService/IISHostingConfigFile.svc?wsdl


    class Test
    {
    static void Main()
    {
    ServiceIntentionsClient client = new ServiceIntentionsClient();

    // Use the 'client' variable to call operations on the service.

    // Always close the client.
    client.Close();
    }
    }



Comments

Popular posts from this blog

IIS 7.5, HTTPS Bindings and ERR_CONNECTION_RESET

Table Per Hierarchy Inheritance with Column Discriminator and Associations used in Derived Entity Types

The <link> saga on ASP .Net