Nick
Nick

Reputation: 7525

Unable to deploy a simple WCF - Get the "The resource cannot be found"

I created a very simple WCF and deployed it to the hosting provider by doing a "Copy Website". Locally, it's setup to use the development server (not in IIS). The domain is under Full trust permissions. Locally, I can get to the Service.svc page, but not in the hosting provider.

The code is as follows:

Service.svc

<%@ ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" Factory="CustomHostFactory" %>

Service.cs

// NOTE: If you change the class name "Service" here, you must also update the reference to "Service" in Web.config and in the associated .svc file.
public class Service : IService
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        if (composite.BoolValue)
        {
            composite.StringValue += "Suffix";
        }
        return composite;
    }
}


class CustomHostFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        // If more than one base address exists then return the second address,
        // otherwise return the first address
        if (baseAddresses.Length > 1)
        {
            return new ServiceHost(serviceType, baseAddresses[1]);
        }
        else
        {
            return new ServiceHost(serviceType, baseAddresses[0]);
        }
    }
}

class CustomHost : ServiceHost
{
    public CustomHost(Type serviceType, params Uri[] baseAddresses)
        : base(serviceType, baseAddresses)
    { }
}

Web.config

  <system.serviceModel>
    <services>
      <service name="Service" behaviorConfiguration="ServiceBehavior">
        <!-- Service Endpoints -->
        <endpoint address="" binding="wsHttpBinding" contract="IService">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above 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="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

Any help?

Upvotes: 0

Views: 2371

Answers (2)

russau
russau

Reputation: 9098

I just ended up with the same issue. Are you using the CustomHostFactory from Rob Zelt's article: WCF: This collection already contains an address with scheme http?

The baseAddresses getting passed in will contain all the bindings set up for IIS; host headers, ports, http/https. The binding you pass to ServiceHost needs to match the URL that the WCF request to coming to - otherwise you'll get "resource cannot be found".

Upvotes: 0

marc_s
marc_s

Reputation: 754538

A few things:

  • did you create a virtual directory at your hosting provider? The *.svc file must be deployed into the root of a virtual directory

  • did you navigate to http://yourserver/virtualdirectory/yourservice.svc ? When hosting in IIS, all the base address stuff is moot and will be ignored - the service address is defined by the web server name, virtual directory, and name of the *.svc file

Upvotes: 1

Related Questions