2

I am trying to host a WCF Service Library on IIS 10, with a self signed SSL.

To Obtain Minimum Complete Verifiable Example, Open Visual studio 2017

New>Project>C#>Web>WCFLibrary With this you will get a simple code that has one operation contract which takes an integer and returns a string.

Now I am trying to host this on IIS with Self Signed SSL (Mine has more operation contract but this will do).

What I have tried so far.

Now the next part is hosting it on IIS, so I created the SVC file

My SVC file contains ->

<%@ ServiceHost Language = "C#" Debug = "true" Service = "WcfServiceLibrary2.Service1" %>

Then all the tutorials that I can find edit Web.Config, which is unavailable in Visual Studio 2017, so I tried two things 1. Created a Web.Config file and added the configurations 2. Published the website and then obtained Web.Config which did not require any changes

Then I went on to IIS (as administrator) and added a new website

Then while trying to browse, to see the message that IIS service is hosted I got this error "Cannot read configuration file" To Solve this I followed success.trendmicro.com/…

Now that error is gone but now I am getting

Forbidden Error

To solve this I Followed IIS - 401.3 - Unauthorized But this lead to the browser letting me browse the directories rather than giving the message that a Service has been created.

Any Idea what I am missing here?

Definitely I am missing something major here as I have failed to host it on HTTP itself, All the tutorials I find online have a file Web.Config and not App.config, I am looking for an example (preferably with Images) that demonstrate it just with this small example.

I know this doesn't follow all SO guidelines on asking questions, but I have failed to articulate it into a question that does.

Edit

As per LexLi's advice that it may already be hosted, I went and tried to consume it using svcutil which gave me the error

WS-Metadata Exchange Error
    URI: http://localhost/Service1.svc

    Metadata contains a reference that cannot be resolved: 'http://localhost/Service1.svc'.

    The remote server returned an unexpected response: (405) Method Not Allowed.

    The remote server returned an error: (405) Method Not Allowed.


HTTP GET Error
    URI: http://localhost/Service1.svc

    There was an error downloading 'http://localhost/Service1.svc'.

    The request failed with HTTP status 404: Not Found.

The Url is correct because I obtained it by using the browse functionality from IIS.

anand_v.singh
  • 2,768
  • 1
  • 16
  • 35
  • "But this lead to the browser letting me browse the directories rather than giving the message that a Service has been created." Why do you need that message? A WCF web service is a web service, which you call via SOAP or REST, and it shows no message (no GET). – Lex Li Apr 01 '19 at 16:28
  • @LexLi without the message, how do I verify that the service has been created? – anand_v.singh Apr 01 '19 at 17:04
  • call the actual web service. – Lex Li Apr 01 '19 at 17:53
  • @LexLi That didn't work, additional Info added in the question sections. – anand_v.singh Apr 02 '19 at 03:07
  • It has tons of possible causes, https://stackoverflow.com/questions/9114870/wcf-test-client-cannot-add-service-cannot-obtain-metadata Your hack of creating a WCF service can be the bad start. If possible, use the right template and start from scratch, https://learn.microsoft.com/en-us/dotnet/framework/wcf/wcf-vs-templates#wcf-service-application-template – Lex Li Apr 02 '19 at 03:54
  • @LexLi The hack is not by choice, in the project it is already working as a WCFServiceLibrary, I am looking into the question you linked, thanks :) – anand_v.singh Apr 02 '19 at 04:15
  • @LexLi I tried switching to WCFServiceApplication, that is hosting in IIS express, but again not in IIS, Right now I am getiing 500.19 error, so I followed https://support.microsoft.com/en-in/help/942055/http-error-500-19-error-when-you-open-an-iis-7-0-webpage, mine is the last error on this page, following that I switched everything to allowed, I also followed https://stackoverflow.com/questions/9794985/config-error-this-configuration-section-cannot-be-used-at-this-path, I am out of Ideas, I was hoping you might have one on what I can try or what is it that I am missing. – anand_v.singh Apr 03 '19 at 04:23
  • @LexLi Figured it out, I was doing tons of things wrong, thanks, a lot of your links helped :) – anand_v.singh Apr 03 '19 at 05:49
  • then post your own answer and accept it. – Lex Li Apr 03 '19 at 10:47
  • @LexLi Done, check it out and let me know if you have any suggestions and/or recommendations. – anand_v.singh Apr 04 '19 at 05:21

3 Answers3

1

First, please don’t set the IIS physic path to desktop, which will cause the permission problem. we could set the IIS site physical path to the folder under C partition.
Second, please read the following link, which mainly indicates that WCF service library project could not published directly to IIS since its Appconfig could not be recognized by IIS unless additional Webconfig is added manually in the root directory of the web site.
https://learn.microsoft.com/en-us/dotnet/framework/wcf/deploying-a-wcf-library-project
Generally speaking, we use the WCF service project template which contains an auto-generated Webconfig file instead of WCF service library project template, since it is generally used as a class library.
enter image description here
By default, the service is hosted with BasicHttpBinding, it depends on the following tag.

<protocolMapping>
        <add binding="basicHttpBinding" scheme="http" />
</protocolMapping>

The service could also be configured manually by the following ways. These two ways configuration file is equivalent.

<system.serviceModel>
    <services>
      <service name="WcfService1.Service1" behaviorConfiguration="mybehavior">
        <endpoint address="" binding="basicHttpBinding" contract="WcfService1.IService1" bindingConfiguration="mybinding"></endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="mybinding">
          <security mode="None">
          </security>
        </binding>
      </basicHttpBinding>
</bindings>

It is unnecessary to assign the service endpoing address in the webconfig. It should be completed in the IIS site binding module.
WCF System.ServiceModel.EndpointNotFoundException
At last, we could host the service over https by adding an additional service endpoint, refer to the following configuration(simplified configuration).

<protocolMapping>
  <add binding="basicHttpBinding" scheme="http"/>
    <add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>

then add the https protocol in IIS site binding module.
Configuring Web.config to publish WCF Service
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-an-iis-hosted-wcf-service-with-ssl
Feel free to let me know if there is anything I can help with.

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22
  • Hi, Thanks for the answer, sadly I am not the one deciding WCFServiceLibrary, and I tried following this link https://learn.microsoft.com/en-us/dotnet/framework/wcf/deploying-a-wcf-library-project that you referenced here, but sadly the instructions on that are quite outdated, the main code is already written in it's template, I will try doing it this way and see if we can port it on this, then try to convince them to switch :) – anand_v.singh Apr 02 '19 at 08:50
  • In the meantime, if you know how it can be done on WCFServiceLibrary, that would be much appreciated. – anand_v.singh Apr 02 '19 at 08:51
  • we are supposed to add an additional web.config file in the physical path of the IIS site. Add the protocol mapping to webconfig and add the http binding to IIS site binding module. – Abraham Qian Apr 02 '19 at 09:21
  • web.config is already generated when I publish this, so I just have to edit that? – anand_v.singh Apr 02 '19 at 10:09
  • Yes, configure the service in the file. – Abraham Qian Apr 02 '19 at 10:15
  • I tried switching to WCFServiceApplication, that is hosting in IIS express, but again not in IIS, Right now I am getiing 500.19 error, so I followed https://support.microsoft.com/en-in/help/942055/http-error-500-19-error-when-you-open-an-iis-7-0-webpage, mine is the last error on this page, following that I switched everything to allowed, I also followed https://stackoverflow.com/questions/9794985/config-error-this-configuration-section-cannot-be-used-at-this-path, I am out of Ideas, I was hoping you might have one on what I can try or what is it that I am missing. – anand_v.singh Apr 03 '19 at 04:24
  • Figured it out, I was doing tons of things wrong, still thanks ;) – anand_v.singh Apr 03 '19 at 05:48
  • admire your practice and the ability to solve problems. Cheers. – Abraham Qian Apr 03 '19 at 06:19
1

First Credit where credit is due, many of the issues I was pointed in the right direction by multiple SO users to name a few would be: Mikael bolinder, Abraham Qian, Lex Li, the wonderful people in the C# chat room and a co-worker of mine(Doesn't have an account on SO as of writing this answer), In this answer I plan to cover everything you might need to host a WCF Library in IIS Server using HTTPS security.

Tools I used:

  • Visual Studio 2017 professional
  • IIS 10 (Comes with windows 10 but has to be activated in windows features ) (See below)

First: Make sure you have all the components you will need from visual studio installed.

  1. Windows -> .Net Desktop Development
  2. Windows -> Universal Windows platfor development
  3. Web & Cloud -> ASP.NET and web development

In this list and among other list that will come, some extra components may be included, the reason for that is I installed them and couldn't verify one way or the another if they are absolutely necessary.

Now, let's add the windows features necessary. Control Panel -> Programs -> Turn Windows features on or off

Windows Features necessary

Make sure to go into WCF Services and check HTTP Activation, don't be fooled by the square block (One of my mistakes)

Now let's get to creating the Service. Open Visual Studio File -> New -> Project -> Visual C# -> Web -> WCF -> WCF Service Library This generates the MCVE that you are trying to host

Now you have to link it with a website in order to generate the Web.Config file along with the SVC file, to do that, On Solutions Explorer, right click on your solution, Add-> New Website.

Now in the web.config file add

<system.serviceModel>
    <services>
      <service name="WcfServiceLibrary4.Service1"> <!-- Change the library name as per your need -->
        <endpoint address=""
                  binding="wsHttpBinding"
                  bindingConfiguration="secureHttpBinding"
                  contract="WcfServiceLibrary4.IService1"/> <!-- Change the library name as per your need -->

        <endpoint address="mex"
                  binding="mexHttpsBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="secureHttpBinding">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpsGetEnabled="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="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

Next add reference to service on the website

Adding Reference

Discover services and add the service you have created. Now Build your solution and publish it. **Careful Don't publish the solution in a user Directory like Desktop or documents or else ACL permissions will give you a headache, rather publish it directly in a directory in Drive.

Now Hosting time First lets open IIS (Always as admin) and create a new certificate.

On the server go to the IIS part and Select Server Certificates, then click create new certificate on the right end.

Create Certificate

Now create a new website from the left menu

Create website

Choose website options

Make sure to switch to https and select your certificate here, Now to verify that your service is created you will need to browse your websites svc file created, sadly at this point you will get an error saying Server Error in '/' Application. Could not find a base address that matches scheme http for the endpoint with binding BasicHttpBinding. Registered base address schemes are [https]. I was unable to find the cause of the error, but I was able to find a bypass of it,

Steps to bypass this error -> Select you website from the menu on the left, on the menu at the right click bindings and also add an HTTP binding, with a different port. After this you will be able to browse the HTTPS version of your svc file.

Adding Binding

Now if you browse the HTTPS link you get the message that the service is created

Service created

Now you can go Ahead and create an application that consumes this service.

What lies ahead

  1. I will try to find a way to do this without adding an additional binding
  2. The Second goal would be to achieve this without adding the extra website.

If and when I achieve these I will update, however these are not my priority right now and may not be added for quite a while, if anything didn't make sense, or you have any ideas of improvement comment below.

anand_v.singh
  • 2,768
  • 1
  • 16
  • 35
0

In short here are the steps :

  1. Create Self-Signed Certificate
    1. Add SSL Binding
    2. Configure Virtual Directory for SSL
    3. Configure WCF Service for HTTP Transport Security

This link is more in depth : https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-an-iis-hosted-wcf-service-with-ssl

Joey Phillips
  • 1,543
  • 2
  • 14
  • 22