Link to home
Start Free TrialLog in
Avatar of Sri
Sri

asked on

Integrating Swagger UI with WCF Service

I am trying to Integrate Swagger with a WCF service by following the article SwaggerWCF

I skipped ASP.Net part as i dont have ASP.Net in my project

The steps are not clearly stating whether these changes have to made in the service or in the host, mine is a self hosted service.

I have tried making changes in both the places (service and host, i am always getting
failed to parse JSON/YAML response swaggerwcf

Please help if i am missing anything

Adding project code

Service Project (WCFSample)

Service Interface

public interface IService1
{
[SwaggerWcfPath("Get Data", "Gets Data")]
[WebInvoke(UriTemplate = "/data",
BodyStyle = WebMessageBodyStyle.Bare,
Method = "Get",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
string GetData(int value);

    [SwaggerWcfPath("Get Data", "Gets Data using data contract")]
    [WebInvoke(UriTemplate = "/datawithcontract",
        BodyStyle = WebMessageBodyStyle.Bare,
        Method = "Get",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);

    // TODO: Add your service operations here
}

// Use a data contract as illustrated in the sample below to add composite types to service operations.
// You can add XSD files into the project. After building the project, you can directly use the data types defined there, with the namespace "WcfSample.ContractType".
[DataContract(Name = "CompositeType")]
[Description("Composite Type")]
[SwaggerWcfDefinition(ExternalDocsDescription = "Composite Type")]
public class CompositeType
{
    bool boolValue = true;
    string stringValue = "Hello ";

    [DataMember(Name = "boolVal")]
    [Description("Boolean Value")]
    public bool BoolValue
    {
        get { return boolValue; }
        set { boolValue = value; }
    }

    
    [DataMember(Name = "stringVal")]
    [Description("String Value")]
    public string StringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }
}

Open in new window


Service Class

public class Service1 : IService1
{
[SwaggerWcfTag("Sample")]
[SwaggerWcfResponse(HttpStatusCode.Created, "Get Data, value in the response body with id updated")]
[SwaggerWcfResponse(HttpStatusCode.BadRequest, "Bad request", true)]
[SwaggerWcfResponse(HttpStatusCode.InternalServerError,
"Internal error (error)", true)]
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}

    [SwaggerWcfTag("Sample")]
    [SwaggerWcfResponse(HttpStatusCode.Created, "Get Data using data contract")]
    [SwaggerWcfResponse(HttpStatusCode.BadRequest, "Bad request", true)]
    [SwaggerWcfResponse(HttpStatusCode.InternalServerError,
        "Internal error (error2)", true)]
    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        if (composite == null)
        {
            throw new ArgumentNullException("composite");
        }
        if (composite.BoolValue)
        {
            composite.StringValue += "Suffix";
        }
        return composite;
    }
}

Open in new window


Host Project ( WCFSwaggerHost)

App.config

  <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="swaggerwcf" type="SwaggerWcf.Configuration.SwaggerWcfSection, SwaggerWcf" />
  </configSections>
  <swaggerwcf>
    <tags>
      <tag name="LowPerformance" visible="false" />
    </tags>
    <settings>
      <setting name="InfoDescription" value="Sample Service to test SwaggerWCF" />
      <setting name="InfoVersion" value="0.0.1" />
      <setting name="InfoTermsOfService" value="Terms of Service" />
      <setting name="InfoTitle" value="SampleService" />
      <setting name="InfoContactName" value="Abel Silva" />
      <setting name="InfoContactUrl" value="http://github.com/abelsilva" />
      <setting name="InfoContactEmail" value="no@e.mail" />
      <setting name="InfoLicenseUrl" value="https://github.com/abelsilva/SwaggerWCF/blob/master/LICENSE" />
      <setting name="InfoLicenseName" value="Apache License" />
    </settings>
  </swaggerwcf>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
  <system.serviceModel>
    <services>
      <service name="WcfSample.Service1">
        <endpoint address="http://0.0.0.0:8090/" binding="webHttpBinding" contract="WcfSample.IService1" />
      </service>
      <service name="SwaggerWcf.SwaggerWcfEndpoint">
        <endpoint address="http://localhost/docs" binding="webHttpBinding" contract="SwaggerWcf.ISwaggerWcfEndpoint" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp automaticFormatSelectionEnabled="true" />
        </behavior>
      </endpointBehaviors>
      <!-- [.......] -->
    </behaviors>
  </system.serviceModel>
</configuration> 

Open in new window


Startup Program
 class Program
    {
        static void Main(string[] args)
        {
            using (var host = new ServiceHost(typeof(Service1)))
            {
                host.Open();
                var swaggerHost = new ServiceHost(typeof(SwaggerWcfEndpoint));
                swaggerHost.Open();
                Console.WriteLine("Press Enter to stop");
                Console.ReadLine();
            }
        }
    } 

Open in new window

Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

Did you make sure that the "Copy to Output Directory" set to "Copy Always" for your yaml file?
Avatar of Sri
Sri

ASKER

yes Shipman
did you enter the route in the Application_Start and check the System.ServiceModel.Activation like it says in the instructions?
Avatar of Sri

ASKER

Yes Shipman
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.