Kyle
Kyle

Reputation: 17687

Can I host a WCF and WebService from the same windows service?

I have WCF service running from within a Windows Service. This is working fine and I am able to send/receive soap messages to the server.

The problem is I would also like to be able to access a static file (a simple download) from this same service (or more specifically from the same port, so that only one port needs to be internet accessible).

With my existing service, I have methods such as:

public interface IMyServerService
{
    [OperationContract]
    [WebInvoke]
    string ListValue(string arg1);
}

I have now tried to add the following:

    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/DownloadFile")]
    System.IO.Stream DownloadFile();

With the following implementation:

    public System.IO.Stream DownloadFile()
    {
        var context = System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse;

        context.Headers.Add(System.Net.HttpResponseHeader.CacheControl, "public");
        context.ContentType = "text/plain";
        context.StatusCode = System.Net.HttpStatusCode.OK;

        return new System.IO.FileStream(@"C:\test.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read);
    } // End of DownloadFile

My goal would be to access https://service/DownloadFile but when I try that I get a error 400.

I guess my questions would be, am I able to do this from inside the same WCF service, or should I create a second service to do so?

Upvotes: 0

Views: 100

Answers (1)

diggingforfire
diggingforfire

Reputation: 3399

You can, provided you set up the correct bindings (like the webHttpBinding for your new function). The question is, should you? Typically a ServiceContract groups together certain functionality. If this file downloading is something new that has nothing to do with the existing service, I'd create a new one.

Upvotes: 1

Related Questions