0

I am trying to create a WCF Restful Service.

Here is my contract: (The ActivateUser class extends the BaseResult class).

namespace SmartShopServerContract {
[ServiceContract]
public interface IService {
    [OperationContract]
    [WebGet(RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json, UriTemplate="au/{eMail}/{password}/{idHandy}")]
    ActivateUserResult ActivateUser(string eMail, string password, string idHandy);
}

// Basisklasse der Resultate --> alle Resultate haben einen definierten Status!
[DataContract]
public abstract class BaseResult {

    private string status;

    public BaseResult(string status) {
        this.status = status;   
    }

    [DataMember]
    public string Status {
        get { return this.status; } 
    }
}

// Result für ActivateUser
[DataContract]
public class ActivateUserResult : BaseResult {
    public ActivateUserResult(string status)
        : base(status) {
    }

    [DataMember]
    public string CryptedPassword { get; set; }
}

}

Here is the implementation of the Service:

namespace SmartShopServerService {
public class ServiceSmartShop : IService {

    public ActivateUserResult ActivateUser(string eMail, string password, string idHandy) {
        return new ActivateUserResult("OK") {
            CryptedPassword="testatsa"
        };
    }

And there is the Web.config file:

    <?xml version="1.0"?>
<configuration>
  <system.serviceModel>
     <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json">
        </standardEndpoint>
      </webHttpEndpoint>
    </standardEndpoints>
    <services>
      <service name="SmartShopServerService.ServiceSmartShop" behaviorConfiguration="RESTBehavior">
        <endpoint address="/" binding="webHttpBinding" contract="SmartShopServerContract.IService" behaviorConfiguration="SmartShopBehavior"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="RESTBehavior">
          <serviceMetadata httpGetEnabled="true" policyVersion="Policy15"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="SmartShopBehavior">
          <webHttp automaticFormatSelectionEnabled="false"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.5" sku=".NETFramework,Version=v4.5"/>
  </startup>
  <system.web>
    <compilation debug="true"/>
  </system.web>
  <system.webServer>
    <modules>
      <remove name="WebDAVModule"/>
    </modules>
  </system.webServer>
</configuration>

I am testing this with the "VS2012 native tool commant prompt" like this:

svcutil.exe http://localhost:51162/SmartShopService.svc/au/data1/data2/data3

The code is being executed, but I am still getting a Method not allowed (405) exception.

Any thoughts on that?

--> currently local use

--> IIS Express (Visual Studio 2012)

6
  • This question and answer might provide some insight. stackoverflow.com/a/545754/1181408 Commented May 6, 2013 at 13:45
  • How are you invoking the http operation? i.e. show the javascript/jquery or WebClient/etc... Commented May 6, 2013 at 13:51
  • 1
    One way to get this error is to invoke with the wrong verb. e.g. you've defined it for GET, if you used POST to invoke that URL then the WebAPI framework to kick back the Method Not Allowed error. Commented May 6, 2013 at 13:52
  • I am already using the [WebGet()] annotation. (see code above). the http operation is being invoked with the vs2012 native tool command prompt (svcutil.exe) --> code above ;). with the svcutil.exe localhost:51162/SmartShopService.svc/au/data1/data2/data3 command i am able to invoke the methode using GET. Commented May 6, 2013 at 14:58
  • It's not clear what you're doing. svcutil is for generating client proxy code to invoke the web verbs--but you've only provided the server-side code that implements the operation. Without knowing how that server code is being invoked, we can't tell what's wrong. You say "I am able to invoke the method using GET", does that mean it works or not? Commented May 6, 2013 at 15:38

2 Answers 2

3

You're using svcutil to create a proxy for a "RESTful WCF service". This does not work. The quick reason is that Web endpoints do not expose metadata for the svcutil tool to know what requests it needs to send to it. The long version is on the linked blog post.

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

2 Comments

thanks for the reply. it is true what you are saying, but this was not the problem (or at least the main problem), because with this configuration it is not working at all. But I found a solution ;)
Glad you found a solution. Can you post it as an answer and accept it so others will learn from what you've been through? Thanks!
0

The problem was the base class (BaseResult) for the ActivateUser class --> BaseResult

It seems like that it is not possible to extend a DataContract class and expect it to work.

Now i am using a Interface instead of a base class

public interface IResult {
    string Status{get;set;}
}


[DataContract]
public class ActivateUserResult : IResult {

    [DataMember]
    public string CryptedPassword { get; set; }

    [DataMember]
    public string Status { get; set; }
}

This is working....thats all i know ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.