WCF Configuration Hell?

前端 未结 7 779
迷失自我
迷失自我 2021-02-08 01:20

I hate WCF setup with endpoints, behaviors etc. I believe all these things should be performed automatically. All I want to do is to return JSON result from my WCF service. Here

相关标签:
7条回答
  • 2021-02-08 01:33

    You are using the WebInvokeAttribute which tells WCF by default to accept POST as the verb. Since you are trying to access it via a GET action, it is being ignored.

    Use WebGetAttribute instead.

    Per MSDN:

    If you want a service operation to respond to GET, use the WebGetAttribute instead.

    0 讨论(0)
  • 2021-02-08 01:35

    I am using .net framework 4, VS2010. I made a dummy mistake in my global.asax.cs that instead of creating an instance of WebServiceHostFactory I punched WebScriptServiceHostFactory through intellSense. As a result I got the same error:

    Endpoints using 'UriTemplate' cannot be used with 'System.ServiceModel.Description.WebScriptEnablingBehavior'.

    I corrected the instance to WebServiceHostFactory in my global.asax.cs, I don't see the error anymore.

    0 讨论(0)
  • 2021-02-08 01:36

    In your WebGet or WebInvoke Specify your BodyStyle to WrappedRequest, then remove the UriTemplate. When Invoking your service use the function name.

    Hope it helps.

    Example:

        [OperationContract]
        [WebInvoke(Method = "POST",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json)]
        void register(string name, string surname);
    
    0 讨论(0)
  • 2021-02-08 01:39

    Take a look at this SO question.

    Edit: Since you're not specifying an address for your service, try hitting: http://localhost:22059/ZombieService.svc/KnownZombies (with the .svc).

    I also think you need the <webHttp /> behavior added to your specified endpoint behavior.

    Edit: Try changing your endpoint definition to this:

    <service 
      name="HighOnCodingWebApps.ZombieService"
      behaviorConfiguration="MyServiceTypeBehaviors">
    
      <endpoint 
        address="" 
        binding="webHttpBinding"
        behaviorConfiguration="SomeBehavior"
        bindingConfiguration="default"
        contract="HighOnCodingWebApps.IZombieService" />
    
    </service>
    
    0 讨论(0)
  • 2021-02-08 01:43

    I personally don't like all of the configuration options that WCF offers (I've ranted about it before), and in your case you don't need to use configuration at all. For a simple service to return JSON, you can use a service host factory, and there's one which does exactly that (set up a webHttpBinding/webHttp behavior endpoint), the System.ServiceModel.Activation.WebServiceHostFactory. In your case, you can:

    • Remove everything from config (really, you don't need config at all)
    • Update your .svc file to reference that factory (see below)

    That's it. Here's what the .svc should look like:

    <%@ ServiceHost Language="C#" Service="HighOnCodingWebApps.ZombieService" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
    

    One more thing: I noticed that your class isn't decorated with [ServiceContract], but you have a [WebGet] attribute in your method in the class. If the interface (IZombieService) is the one decorated with [ServiceContract], then the method in the interface should be the one decorated with [WebGet]. You can also bypass the interface completely and decorate the ZombieService class with `[ServiceContract] as well.

    [ServiceContract]
    public class ZombieService
    {
        [WebGet(ResponseFormat = WebMessageFormat.Json,
                UriTemplate = "KnownZombies")]
        public Zombie GetZombie()
        {
           return new Zombie() { Name = "Mohammad Azam"};
        }
    }
    
    public class Zombie
    {
        public string Name { get; set; }
    }
    
    0 讨论(0)
  • 2021-02-08 01:46

    Just use ASP.NET Web API instead of WCF

    0 讨论(0)
提交回复
热议问题