Search code examples
asp.netwcf

Server Error in '/' Application when I am running the simple method in WCF Service


[when i add the method test in u-r-l it says error in server application] [this is what's inside my W.C.F service and I am trying to view method test][3]

    public string test()
    {
        // Add your operation implementation here
        return "test...";
    }

Below it the error https://i.sstatic.net/EdFt9.png


Solution

  • If you want to directly access your WCF operation in the browser, you can refer to the below configuration.
    Service Contract.

        public interface IService1
        {
            [OperationContract]
            [WebGet]
            string GetData(int value);
    }
    

    Service.svc

    public string GetData(int value)
            {
                return DateTime.Now.ToLongTimeString();
            }
    

    Web Configuration, just replace the System.servicemodel section.

      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior>
              <webHttp/>
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <protocolMapping>
            <add binding="webHttpBinding" scheme="http" />
        </protocolMapping>    
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
    

    Result.
    enter image description here
    For more details about how to create a basic WCF Web HTTP service.
    https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-create-a-basic-wcf-web-http-service
    Feel free to let me know if there is anything I can help with.