Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6228365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:22:34+00:00 2026-05-24T09:22:34+00:00

I’ve been ripping my hair out for a while now. Can someone provide a

  • 0

I’ve been ripping my hair out for a while now. Can someone provide a really simple example (or a link to a working example) of how to upload a file to a WCF service hosted in IIS.

I’ve started with something simple. I want to call a URL from a client via POST, pass the name of the file and send the file as well. So I added the following to the contract:

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/UploadFile?fileName={fileName}")]
void Upload(string fileName, Stream stream);

The implemented it in the .svc file:

public void Upload(string fileName, Stream stream)
{
    Debug.WriteLine((fileName));
}

Immediately, I get an error upon running the project:

For request in operation Upload to be a stream the operation must have a single parameter whose type is Stream.

Not sure where to go from here. Would love to see an actual working example.

P.S. I did this in .NET 4 with WCF 4 and it seemed a lot simpler, but I’ve had to downgrade. In .NET 3.5, I am missing something.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:22:35+00:00Added an answer on May 24, 2026 at 9:22 am

    In order for it to work you need to define an endpoint with a binding compatible with WebHttpBinding and which has the WebHttpBehavior added to it. The message may be a red herring, it’s an old bug which if you browse to the service base address, and if you have metadata enabled, it will show up. Another issue is that if you want to be able to upload any file type (including JSON and XML), you’ll need to define a WebContentTypeMapper to tell WCF not to try to understand your file (more info at http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx).

    This is a complete example which does that. The biggest problem with 3.5 is that the ContentTypeMapper property doesn’t exist on the WebHttpBinding, so you need to use a custom binding for that. This code defines the endpoint using a custom ServiceHostFactory, but it could be defined using config as well.

    Service.svc

    <%@ ServiceHost Language="C#" Debug="true" Service="MyNamespace.MyService" Factory="MyNamespace.MyFactory" %>
    

    Service.cs

    using System;
    using System.Diagnostics;
    using System.IO;
    using System.ServiceModel;
    using System.ServiceModel.Activation;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Description;
    using System.ServiceModel.Web;
    
    public class MyNamespace
    {
        [ServiceContract]
        public interface IUploader
        {
            [OperationContract]
            [WebInvoke(Method = "POST", UriTemplate = "/UploadFile?fileName={fileName}")]
            void Upload(string fileName, Stream stream);
        }
    
        public class Service : IUploader
        {
            public void Upload(string fileName, Stream stream)
            {
                Debug.WriteLine(fileName);
            }
        }
    
        public class MyFactory : ServiceHostFactory
        {
            protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
            {
                return new MyServiceHost(serviceType, baseAddresses);
            }
    
            class MyRawMapper : WebContentTypeMapper
            {
                public override WebContentFormat GetMessageFormatForContentType(string contentType)
                {
                    return WebContentFormat.Raw;
                }
            }
    
            public class MyServiceHost : ServiceHost
            {
                public MyServiceHost(Type serviceType, Uri[] baseAddresses)
                    : base(serviceType, baseAddresses) { }
    
                protected override void OnOpening()
                {
                    base.OnOpening();
    
                    CustomBinding binding = new CustomBinding(new WebHttpBinding());
                    binding.Elements.Find<WebMessageEncodingBindingElement>().ContentTypeMapper = new MyRawMapper();
                    ServiceEndpoint endpoint = this.AddServiceEndpoint(typeof(IUploader), binding, "");
                    endpoint.Behaviors.Add(new WebHttpBehavior());
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have been unable to fix a problem with Java Unicode and encoding. The
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.