Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i Consume WCF Data Service?

i have created a wcf service but i have used 3 projects for it;
1) ServiceLibrary (WCF library)
2) Web
3) ConsoleTestClient
my ServiceLibrary app.config file looks like this;

  <system.serviceModel>
    <services>
      <service name="MrDAStoreJobs.ServiceLibrary.AdvertisementService">
        <clear />
        <endpoint address="basic" 
                  binding="basicHttpBinding" bindingConfiguration="" 
                  contract="MrDAStoreJobs.ServiceLibrary.Interface.IAdvertisementService" />
        <endpoint name="mexHttpBinding"
          contract="IMetadataExchange"
          binding="mexHttpBinding"
          address="mex" />

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:13758/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel> <br />

Now, to host this library, i have done the following settings in my Web.Config file of the Web Project.
The svc file name is WcfDataService1.svc

    public class WcfDataService1 : DataService<AdvertisementService>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.UseVerboseErrors = true;
ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
        }
    }
  <system.serviceModel>
    <services>
      <service name="MrDAStoreJobs.ServiceLibrary.AdvertisementService">
        <clear />
        <endpoint address="basic" 
                  binding="basicHttpBinding" bindingConfiguration="" 
                  contract="MrDAStoreJobs.ServiceLibrary.Interface.IAdvertisementService" />
        <endpoint name="mexHttpBinding"
          contract="IMetadataExchange"
          binding="mexHttpBinding"
          address="mex" />

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:13758/WcfDataService1.svc" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

Now, when i test this service using directly (ServiceLibrary project) using WCF test client, i see the following and works everything great; enter image description here
The issue is when i try to run my Web project(which i use as a host for wcf service). And then go to the console test client and want to add reference using add reference. I don't see my Get and Set methods (like test client) enter image description hereWhy i don't see my IAdvertisementService interface and the methods
Do i have to deploy this to actuall IIS?

like image 591
Idrees Khan Avatar asked Feb 17 '23 07:02

Idrees Khan


1 Answers

To develop a service using ASP.NET, we must add the WebService attribute to the class and WebMethodAttribute to any of the class methods.

Example

[WebService] 
 public class Service : System.Web.Services.WebService 
  { 
  [WebMethod] 
  public string Test(string strMsg) 
  { 
      return strMsg; 
  } 
 }

To develop a service in WCF, we will write the following code:

[ServiceContract] 
public interface ITest 
{ 
   [OperationContract] 
   string ShowMessage(string strMsg); 
 } 


public class Service : ITest 
   { 
       public string ShowMessage(string strMsg) 
       { 
          return strMsg; 
       } 
   }

The ServiceContractAttribute specifies that an interface defines a WCF service contract, OperationContract attribute indicates which of the methods of the interface defines the operations of the service contract.

A class that implements the service contract is referred to as a service type in WCF.

Hosting the Service

ASP.NET web services are compiled into a class library assembly and a service file with an extension .asmx will have the code for the service. The service file is copied into the root of the ASP.NET application and Assembly will be copied to the bin directory. The application is accessible using URL of the service file.

WCF Service can be hosted within IIS or WindowsActivationService.

Compile the service type into a class library Copy the service file with an extension .SVC into a virtual directory and assembly into bin sub directory of the virtual directory. Copy the web.config file into the virtual directory.

Client Development

Clients for the ASP.NET Web services are generated using the command-line tool WSDL.EXE.

WCF uses the ServiceMetadata tool (svcutil.exe) to generate the client for the service.

For more detail goto this link http://www.codeproject.com/Articles/139787/What-s-the-Difference-between-WCF-and-Web-Services

like image 192
Saddam Mohsen Avatar answered Mar 03 '23 12:03

Saddam Mohsen