Is it possible to create an ASP.NET web service fully contained in .svc file?

Is it possible to create a "drop-in" web service? What I would like to do is create an .svc file that can be placed into a web directory on IIS and accessed (and executed) via its URL, without having to install any other files and without changing any configuration files.

I am investigating a possible security problem in a web application that allows uploading .svc files. If it is possible to upload and then execute an .svc file, this would be a big security problem for this application. Unfortunately I am not a .NET developer, so I am probably missing a lot of things here.

I know that it is possible to put the service interface and implementation straight into the the .svc file after the "Service Host" directive. Here is my .svc file:

<%@ ServiceHost Language = "C#" Debug = "true" Service = "EchoService" %>
public interface IEchoService
{
    string Hello();
}

public class EchoService : IEchoService
{
    public string Hello()
    {
        return "Hello";
    }
}

Now when I access http://localhost/test1.svc I get an exception from .NET: Service 'EchoService' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.

I understand that normally you are supposed to configure web service end points in web.config or app.config file. From some Googling I have also learned that it is possible to configure endpoints programmatically by defining your own ServiceHost and ServiceCodeFactory. I have found some examples, but couldn't get them working.

I would really appreciate if somebody can either tell me "No, what you are trying to do is impossible, because..." or show how I can add the necessary code to the example above to have an executable service.

Update: using Josh's suggestion I finally got it working. See my answer below for the working code.

Answers


Okay, I got a working SOAP web service contained in an .svc file. Here is the code:

<%@ ServiceHost 
    Language = "C#" 
    Debug = "true" 
    Factory="Hello.CustomServiceHostFactory" 
    Service = "Hello.HelloService" %>

using System;
using System.ServiceModel;
using System.ServiceModel.Activation;

namespace Hello
{

    public class CustomServiceHostFactory : ServiceHostFactory
    {

   public override System.ServiceModel.ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
   {
       return base.CreateServiceHost(constructorString, baseAddresses);
   }

       protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
       {
       CustomHost host = new CustomHost(serviceType, baseAddresses);

           //configure customServiceHost here

       host.AddServiceEndpoint(typeof(IHelloService), new BasicHttpBinding(), "");

           var smb = host.Description.Behaviors.Find <System.ServiceModel.Description.ServiceMetadataBehavior>(); 
           if(smb == null)
           {
           // add the "get metadata" behavior 
           // This will allow the service to emit WSDL when tickled at the right HTTP endpoint
           smb= new System.ServiceModel.Description.ServiceMetadataBehavior();
           smb.HttpGetEnabled = true;
           host.Description.Behaviors.Add(smb);
           }    
               return host;
       }
    }

public class CustomHost : ServiceHost
{

  public CustomHost()
  {
  }

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


  public CustomHost(object singeltonInstance, params Uri[] baseAddresses)
        : base(singeltonInstance, baseAddresses)
{
} 

  protected override void ApplyConfiguration()
  {
     base.ApplyConfiguration();
  }
}

[ServiceContract(Namespace="http://www.example.com")]
public interface IHelloService
{
[OperationContract]
   string Hello();
}

public class HelloService : IHelloService
{
   public string Hello() {
        return "Hello";
   }
}
}

It works on .NET 4, not sure about the previous versions.


Need Your Help

How to inject Javascript in the WP7 WebBrowser control?

c# javascript silverlight windows-phone-7

I can inject JavaScript in WebBrowser control in C# windows form by this link

How to Pass Array from One Servlet to Another Servlet?

java arrays servlets

I want to pass multiple values from one servlet to another one servlet. Please tell me how to pass that?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.