0

I know there are few articles on the internet (including here), but after few hours, still can not find the solution to a problem. I have a service for uploading files to the server. And I am getting http error 405, method not allowed. Tried everything without a success so far. Below is the code of service.

Service contract:

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. [ServiceContract(Name = "ExcelUpload.IUploadFile")] public interface IUploadFile { [OperationContract] [DataContractFormat] [WebInvoke(Method = "POST", UriTemplate = "Upload/", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)] ReturnValue FileUpload(Stream File); // TODO: Add your service operations here } // Use a data contract as illustrated in the sample below to add composite types to service operations. [DataContract] public class ReturnValue { [DataMember] public bool IsSuccessfull { get; set; } } 

Service class:

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging. [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerCall, IgnoreExtensionDataObject = true, IncludeExceptionDetailInFaults = true)] public class UploadFile : IUploadFile { public ReturnValue FileUpload(Stream File) { using(FileStream writer = new FileStream(@"C:\temp", FileMode.Create)) { int ReadCount = 0; var buffer = new byte[8192]; while ((ReadCount = File.Read(buffer, 0, buffer.Length)) != 0) { writer.Write(buffer, 0, ReadCount); } } return new ReturnValue() { IsSuccessfull = true }; } } 

Web config

<configuration> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5"/> </system.web> <system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"> <serviceActivations> <add relativeAddress="~/UploadFile.svc" service="ExcelUpload.UploadFile"/> </serviceActivations> </serviceHostingEnvironment> <bindings> <webHttpBinding> <binding name="crossDomain" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="Streamed" sendTimeout="00:05:00" crossDomainScriptAccessEnabled="true"> <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/> <security mode="None" /> </binding> </webHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="defaultServiceBehaviour"> <!-- To avoid disclosing metadata information, set the values below to false before deployment --> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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="true"/>s <serviceThrottling maxConcurrentCalls="100" maxConcurrentInstances="100" maxConcurrentSessions="100"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="defaultEndpointBehaviour"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> <services> <service name="ExcelUpload.UploadFile" behaviorConfiguration="defaultServiceBehaviour"> <endpoint address="" behaviorConfiguration="defaultEndpointBehaviour" bindingConfiguration="crossDomain" binding="webHttpBinding" contract="ExcelUpload.IUploadFile"></endpoint> </service> </services> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <!-- To browse web app root directory during debugging, set the value below to true. Set to false before deployment to avoid disclosing web app folder information. --> <directoryBrowse enabled="true"/> </system.webServer> </configuration> 

Corrections to web.config

<security> <requestFiltering allowHighBitCharacters="true"> <verbs allowUnlisted="false"> <add verb="POST" allowed="true"/> <add verb="GET" allowed="true"/> </verbs> </requestFiltering> </security> 
6
  • assuming you are using IIS to host the service, may I ask if you are sure that the http POST method is allowed in the Website settings? Commented Jul 7, 2014 at 11:20
  • Yes I have IIS to host the service. Where can I enable http POST on IIS? Is it possible threw web.config? I added this line of code to web.config, but still the same error <security> <requestFiltering allowHighBitCharacters="true"> <verbs allowUnlisted="false"> <add verb="POST" allowed="true"/> <add verb="GET" allowed="true"/> </verbs> </requestFiltering> </security> Commented Jul 7, 2014 at 11:42
  • by default IIS should allow those verbs, and generally you don't need those settings to enable POST. If the IIS is not under your control, you may seek assistant from system admin, or you can test the POST capability using cURL or httpie. Commented Jul 7, 2014 at 12:55
  • I have access to the server. I'we also set POST and GET on the IIS side. Didn't work. Then I'we changed inside the Service Contract WebInvoke method from "POST" to "*". I think it works now, but now I am getting the access denied for writing file to folder (added all the permissions to a folder for corresponding AppPool and no luck so far). But this is another thread. Thank you for your help! Commented Jul 7, 2014 at 13:01
  • Ensure that you set UseAppPoolCredentials for your hosted application that uses the AppPool. Commented Jul 7, 2014 at 13:06

2 Answers 2

1

Solution to this problem was to change the WebInvoke method from "POST" to "*" in the service contract declaration.

Sign up to request clarification or add additional context in comments.

1 Comment

Hello, CHanging the method to *worked, however, what this means and what is the effect of it?
0

My WCF service was also giving System.ServiceModel.ProtocolException {"The remote server returned an unexpected response: (405) Method Not Allowed."} and the solution was to add a trailing slash (/) to my endpoint address="http://myhost/My.Service/" The complete endpoint is

 <endpoint address="http://myhost/My.Service/" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding" contract="My.Interfaces.IService" name="myhost_IService_Client_Endpoint" /> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.