Hosting a WCF web service on DiscountASP servers

Discussion in 'ASP.NET 2.0' started by Aristotle, Mar 21, 2007.

  1. These instructions will help you create an IIS-hosted WCF web service. See the attachment for the complete source code of this sample WCF web service. This is a very basic example, but the important part is creating your own custom ServiceHostFactory.

    Recommended resources for WCF:
    .NET 3.0 site - http://wcf.netfx3.com/
    MSDN - http://msdn2.microsoft.com/en-us/library/ms735119.aspx

    Prerequisites:
    .NET 3.0 Framework
    Visual Studio 2005 extensions for .NET Framework 3.0 (WCF & WPF), November 2006 CTP. Download at http://www.microsoft.com/downloads/details.aspx?FamilyId=F54F5537-CC86-4BF5-AE44-F5A1E805680D&displaylang=en.


    1. In Visual Studio 2005, create a New Web Site, and choose the WCF Service template.

    2. Your DiscountASP site is configured with multiple host headers in IIS. Therefore, it will be necessary to override the default ServiceHostFactory in order to specify the right base address of the service. Otherwise, you'll get the error:

    "This collection already contains an address with scheme http. There can be at most one address per scheme in this collection."


    --or--


    "HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly."
    Open the service's code file in the App_Code directory.

    Add the namespace System.ServiceModel.Activation. For example:


     

    Attached Files:

  2. I tried copying your example exactly (other than the domain name) but I get the following error when trying to access the service:

    "The CLR Type 'MyServiceHostFactory' could not be loaded during service compilation. Verify that this type is either defined in a source file located in the application's \App_Code directory, contained in a compiled assembly located in the application's \bin directory, or present in an assembly installed in the Global Assembly Cache. Note that the type name is case-sensitive and that the directories such as \App_Code and \bin must be located in the application's root directory and cannot be nested in subdirectories."


    Any ideas what I am doing wrong?
     
  3. Make sure that you have placed the class file in the App_Code folder due to the following reference
    <%@ServiceHost Language=C# Debug='true' Service='MyService' Factory='MyServiceHostFactory' CodeBehind='~/App_Code/Service.cs'</font> %>

    Infact try running the attached sample application (WCFService.zip )
     
  4. Thanks. Figured it out. When you publish a website with VSStudio 2005
    it creates an App_Code.dll file by default andputs it ina "bin" directory, NOT the App_Code directory, so of course my reference in the .svc file was wrong.

    One other thing. When I tried it I got an error saying user could not be authenticated.
    I simple swithed the endpoit settings to use BasicHTTP and everything worked fine.

    Out of curiosity I would like to know about the authentication error. Is is working like it should? Do I need to put something in my client application (config file?) to access the service?

    P.S. - just wanted to say how pleased I am with my service with DiscountASP. You guys are great! Thanks for keeping up so fast with the new technology. Its really nice to be able to play with the new stuff and then actually have a live siteto put it on.
     
  5. Does anyone have a working WCF HelloWorld Service using wsHttpBinding running on DiscountASP?

    I too am getting a "The caller was not authenticated by the service" when using wsHttpBinding.

    I think getting a workingwsHttpBinding sample would becritically important for all of us developers due to its built-in security. No one wants to send plain text over WebServices/BasisHttpBinding and not everyone wants to pay for https just to encrypt Webservices.
    wsHttpBinding is, by far, the best choice for the simplicity of combining: (Webservices+Security+IIS6.0) ...at least until we switch toIIS7.0.

    Since you friendly cats at DASP are Microsoft Gold Partners, would it be possible to get a working solution for a HelloWorld sample using WCF + wsHttpBinding?

    Thanks so much! Love DASP!
     
  6. RedDog said...

    Does anyone have a working WCF HelloWorld Service using wsHttpBinding running on DiscountASP?

    I too am getting a "The caller was not authenticated by the service" when using wsHttpBinding.

    I think getting a workingwsHttpBinding sample would becritically important for all of us developers due to its built-in security. No one wants to send plain text over WebServices/BasisHttpBinding and not everyone wants to pay for https just to encrypt Webservices.
    wsHttpBinding is, by far, the best choice for the simplicity of combining: (Webservices+Security+IIS6.0) ...at least until we switch toIIS7.0.

    Since you friendly cats at DASP are Microsoft Gold Partners, would it be possible to get a working solution for a HelloWorld sample using WCF + wsHttpBinding?

    Thanks so much! Love DASP!
    I'm having the same issue, and would really like to us wsHttpBinding. Any help on this from DASP? I should mention that I am trying to access this WCF service from a windows client application. I have read that you can set the credentials in the client app to get around this, but not sure if we can do that on DiscountASP or not.[/quote]
     
  7. Creating a custom ServiceHost isn't necessary. The only code you need is:

    publicclassMyServiceHostFactory:ServiceHostFactory
    {
    protectedoverrideServiceHostCreateServiceHost(TypeserviceType,Uri[]baseAddresses)
    {
    //SpecifytheexactURLofyourwebservice
    UriwebServiceAddress=newUri(http://www.domain.com/WCFService/Service.svc);

    ServiceHostwebServiceHost=newServiceHost(serviceType,webServiceAddress);
    returnwebServiceHost;
    }
    }


    It's also probably better to load the URI from application settings:


    publicclassMyServiceHostFactory:ServiceHostFactory
    {
    protectedoverrideServiceHostCreateServiceHost(TypeserviceType,Uri[]baseAddresses)
    {
    //SpecifytheexactURLofyourwebservice
    UriwebServiceAddress=newUri(ConfigurationManager.AppSettings["MyServiceUri"]);

    ServiceHostwebServiceHost=newServiceHost(serviceType,webServiceAddress);
    returnwebServiceHost;
    }
    }


    You'll need to add an appropriate section to your web.config:


    <appSettings>


    <add key="ServiceUri" value="http://www.domain.com/WCFService/Service.svc"/>


    </appSettings>


    Hope this helps.
     
  8. Does anybody know how to implement this when using vb.net???
     
  9. I think I've managed to convert the main two functions above from c# to vb.net...



    Public Class MyServiceHostFactory



    Inherits ServiceHostFactory


    Protected Overrides Function CreateServiceHost(ByVal serviceType As Type, ByVal baseAddresses As Uri()) As ServiceHost



    'Specify the exact URL of your web service


    Dim webServiceAddress As New Uri("http://www.defaultwebsite.com/Service1.svc")


    Dim webServiceHost As New MyServiceHost(serviceType, webServiceAddress)


    Return webServiceHost</BLOCKQUOTE>


    End Function</BLOCKQUOTE>


    End Class


    Public Class MyServiceHost



    Inherits ServiceHost


    Public Sub New(ByVal serviceType As Type, ByVal baseAddresses As Uri)



    MyBase.New(serviceType, baseAddresses)</BLOCKQUOTE>


    End Sub


    Protected Overrides Sub ApplyConfiguration()



    MyBase.ApplyConfiguration()</BLOCKQUOTE>


    End Sub</BLOCKQUOTE>


    End Class</BLOCKQUOTE>


    *Note: I've substituted 'http://www.defaultwebsite.com/Service1.svc' For my webservice address. *


    However, for some reason, when I try to add the Factory attribute to the svc file, I get a warning.... "Attribute 'Factory' is not a valid attribute of 'ServiceHost'.


    Then, when I publish and attempt to access my service I recieve the following error...



    "The CLR Type 'BuyMyPhotosWCF.MyServiceHostFactory' could not be loaded during service compilation. Verify that this type is either defined in a source file located in the application's \App_Code directory, contained in a compiled assembly located in the application's \bin directory, or present in an assembly installed in the Global Assembly Cache. Note that the type name is case-sensitive and that the directories such as \App_Code and \bin must be located in the application's root directory and cannot be nested in subdirectories."</BLOCKQUOTE>
    My svc file is as follows...

    <%@ ServiceHost Language="VB" Debug="true" Service="BuyMyPhotosWCF.Service1" Factory="BuyMyPhotosWCF.MyServiceHostFactory" CodeBehind="Service1.svc.vb" %>
    </BLOCKQUOTE>
    Anybody got any ideas???
    Thanks in advance!

    Post Edited (PGallagher69) : 4/1/2008 11:07:32 AM GMT
     
  10. Ok... cracked it...


    I first had to add the assembly name to my factory in my svc file...


    <%@ ServiceHost Language="VB" Debug="true" Service="BuyMyPhotosWCF.Service1" Factory="BuyMyPhotosWCF.MyServiceHostFactory" CodeBehind="Service1.svc.vb" %>
    I also had to make sure that the new overriding classes were placed outside of my Service1 Class...

    Once I had done this, I was then faced with an error reporting that I hadn't initialized my Description, so I had to do this while initializing the Host Base, as well as overriding the InitializeRuntime function...




    Public Class MyServiceHostFactory



    Inherits ServiceHostFactory


    Public Overrides Function CreateServiceHost(ByVal constructorString As String, ByVal baseAddresses() As System.Uri) As System.ServiceModel.ServiceHostBase



    Return MyBase.CreateServiceHost(constructorString, baseAddresses)</BLOCKQUOTE>


    End Function


    Protected Overrides Function CreateServiceHost(ByVal serviceType As System.Type, ByVal baseAddresses() As System.Uri) As System.ServiceModel.ServiceHost



    Dim webServiceAddress = New Uri("http://www.defaultwebsite.com/Service1.svc")


    Dim webServiceHost = New MyServiceHost(serviceType, webServiceAddress)


    Return webServiceHost</BLOCKQUOTE>


    End Function</BLOCKQUOTE>


    End Class


    Public Class MyServiceHost



    Inherits ServiceHost


    Public Sub New(ByVal serviceType As Type, ByVal baseAddresses As Uri)



    Dim BaseAddressScheme As New UriSchemeKeyedCollection


    BaseAddressScheme.Add(baseAddresses)


    MyBase.InitializeDescription(serviceType, BaseAddressScheme)</BLOCKQUOTE>


    End Sub


    Protected Overrides Sub ApplyConfiguration()



    MyBase.ApplyConfiguration()</BLOCKQUOTE>


    End Sub


    Protected Overrides Sub InitializeRuntime()



    MyBase.InitializeRuntime()</BLOCKQUOTE>


    End Sub</BLOCKQUOTE>


    End Class


    *Note: I've substituted 'http://www.defaultwebsite.com/Service1.svc' For my webservice address. *


    And, low and behold, my WCF Service now works correctly. Yey!
     
  11. Awesome!
    Very much appreciate you posting the followup. [​IMG]
     
  12. So I had the same problems everyone else here did, and more but all of the solutions were fragments that took a bit of digging to get wired up.

    A working solution in VB.NET using wsHTTPBinding is fully documented with source at http://www.skysanders.net/WCFHosting/Default.aspx

    Source is attached
     

    Attached Files:

  13. Great work Sky! Well done!
     
  14. Bruce

    Bruce DiscountASP.NET Staff

    Thanks Sky for the post!
     
  15. hi guys...

    i tried to load the zip file from Aristotle to my webspace... but i still get an error message.

    http://www.dotlive.de/test/Service.svc

    i changed the service.cs file and added my uri

    Uri webServiceAddress = new Uri(http://www.dotlive.de/test/Service.svc);

    then i loaded everythinn to my webspace in the folder web/test


    but i still get this error message and i have no idea...?!

    please help me...
     
  16. set customErrors mode='Off' in your web.config to see the error message.
     
  17. SSL Certificate need to be installedfor using WCF service with wsHttpBinding on discountasp.net. I have oneworking.If you need more details then send me a letter at [email protected]

    Best Regards

    Vitaly Laskarzhevsky
     
  18. I've got this error after upload the sample file to discountASP server, any ideas?
    c:\windows\system32\inetsrv> "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe" /t:library /utf8output /R:"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.Web.Mobile\2.0.0.0__b03f5f7f11d50a3a\System.Web.Mobile.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll" /R:"C:\WINDOWS\assembly\GAC_32\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll" /R:"C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll" /R:"C:\WINDOWS\assembly\GAC_32\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.Web.Services\2.0.0.0__b03f5f7f11d50a3a\System.Web.Services.dll" /out:"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\wcfservice\3266d2a5\18a1673d\App_Code.8-cw4k0t.dll" /D:DEBUG /debug+ /optimize- /w:4 /nowarn:1659;1699;1701 "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\wcfservice\3266d2a5\18a1673d\App_Code.8-cw4k0t.0.cs" "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\wcfservice\3266d2a5\18a1673d\App_Code.8-cw4k0t.1.cs"


    Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.1433
    for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
    Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.
    warning CS1668: Invalid search path 'C:\Program Files\SQLXML 3.0\bin\' specified in 'LIB environment variable' -- 'Access is denied. '
    e:\web\projectwatc\htdocs\WCFService\App_Code\Service.cs(2,14): error CS0234: The type or namespace name 'ServiceModel' does not exist in the namespace 'System' (are you missing an assembly reference?)
    e:\web\projectwatc\htdocs\WCFService\App_Code\Service.cs(3,14): error CS0234: The type or namespace name 'ServiceModel' does not exist in the namespace 'System' (are you missing an assembly reference?)
    e:\web\projectwatc\htdocs\WCFService\App_Code\Service.cs(52,37): error CS0246: The type or namespace name 'ServiceHostFactory' could not be found (are you missing a using directive or an assembly reference?)
    e:\web\projectwatc\htdocs\WCFService\App_Code\Service.cs(64,30): error CS0246: The type or namespace name 'ServiceHost' could not be found (are you missing a using directive or an assembly reference?)
    e:\web\projectwatc\htdocs\WCFService\App_Code\Service.cs(54,24): error CS0246: The type or namespace name 'ServiceHost' could not be found (are you missing a using directive or an assembly reference?)
    e:\web\projectwatc\htdocs\WCFService\App_Code\Service.cs(14,6): error CS0246: The type or namespace name 'OperationContract' could not be found (are you missing a using directive or an assembly reference?)
    e:\web\projectwatc\htdocs\WCFService\App_Code\Service.cs(16,6): error CS0246: The type or namespace name 'OperationContract' could not be found (are you missing a using directive or an assembly reference?)
    e:\web\projectwatc\htdocs\WCFService\App_Code\Service.cs(11,2): error CS0246: The type or namespace name 'ServiceContract' could not be found (are you missing a using directive or an assembly reference?)
    e:\web\projectwatc\htdocs\WCFService\App_Code\Service.cs(38,6): error CS0246: The type or namespace name 'DataMember' could not be found (are you missing a using directive or an assembly reference?)
    e:\web\projectwatc\htdocs\WCFService\App_Code\Service.cs(44,6): error CS0246: The type or namespace name 'DataMember' could not be found (are you missing a using directive or an assembly reference?)
    e:\web\projectwatc\htdocs\WCFService\App_Code\Service.cs(32,2): error CS0246: The type or namespace name 'DataContract' could not be found (are you missing a using directive or an assembly reference?)

    <script type=text/javascript>
    function OnToggleTOCLevel1(level2ID)
    {
    var elemLevel2 = document.getElementById(level2ID);
    if (elemLevel2.style.display == 'none')
    {
    elemLevel2.style.display = '';
    }
    else {
    elemLevel2.style.display = 'none';
    }
    }
    </script>
     
  19. Did you upload the web.config? You should modify the web.config that your Visual Studio 2005 project created, assuming that you're using the WCF templates.
     
  20. Thanks, I got it working in VS2008 after following comparin's advice above, and I am completely new to WCF and SOA.

    It is a lot easier than some make it out to be.

    I did have to switch to basicHttpBinding though. Is it true that one must have SSL on DiscountASP to get wsBinding to work?
     
  21. Not necessarily. SSL is just one of many ways to secure a WCF service. An SSL certificate is generally used at the transport level, and it can be used with basicHttpBinding and wsHttpBinding.
     
  22. Does anyone try wsDualHttpBinding (duplex binding) on DiscountASP? /emoticons/hop.gif
     
  23. Hi!

    Is there a way to use webHttp with WCF? I am trying to build a WCF service that accepts and consumes JSON data. I have the service up and running locally but I run into all kind of troubles when I try to deploy it to discountasp.net server. My guess is this is to do with webHttp binding and WebSErviceHostFactory. Here is my thread

    http://community.discountasp.net/default.aspx?f=33&m=27724

    Please help!
     
  24. I'm trying to do the same thing with DataServiceHostFactory to work with a ADO.NET Data Service:

     
  25. If I want to use wsHttpBinding, and I don't have an SSL certificate, then how can I get it to work without using an SSL certificate?

    Thanks
    George
     
  26. Hi there,

    The above works well with services I create myself, but I'm also trying to run the asp.net authentication, profile and role functionality as a service.
    Since I don't have access to the code behind for these though I can't work arround the above problem, any ideas?

    Just to clarify I have a svc file with no code behind and the following mark up in it:


    <%@ ServiceHost Language="C#" Service="System.Web.ApplicationServices.AuthenticationService" %>


    Thanks
     
  27. vah

    vah

    Hi There;

    I had my webservice working fine over HTTP without doing any extra coding for the ServiceHostFactory and so on. I had to add the following in my web.config for it to work

    <serviceHostingEnvironment>

    <add prefix='http://www.mydomain.com'/>
    </baseAddressPrefixFilters>
    </serviceHostingEnvironment>

    Now I needed to use it over HTTPS. I got it to a point where https://www.mydomain.com/services/myservice.svc?wdsl worked great but within it all the URLs were https://web107.dotnetplaground.com so it wasn't that great since when I try to consume it I got cannot resolve https://web107.dotnetplaground.com. At this point I asked discountasp.net support and they told referred me to this post as I needed to programmatically change the base address they said. I just did it programmatically but now I get the error Could not find a base address that matches scheme https for the endpoint with binding BasicHttpBinding. Registered base address schemes are [http]. I've been trying to get my service working over https for at least 3 weeks with no luck can anyone pleeeeeeeezzzzze help me.
     
  28. What does your entire <system.serviceModel> section look like?

     
  29. I'm getting the following error with the provided sample:


    Parser Error
    Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

    Parser Error Message: The process cannot access the file '***MY DOMAIN**\App_Code\Service.cs' because it is being used by another process.
    Source Error:
    [No relevant source lines]



    EDITED: I take my word back it works. I had first wrong service name in Service.cs. When I changed it, I could not upload the new cs-file via ftp. I made then another service which works.
     

Share This Page