简体   繁体   中英

WCF REST Question, Binding, Configuration

I am working on a WCF rest interface using json. I have wrapped the service in a windows service to host the service but I am now having trouble getting the service to be callable. I am not sure exactly what is wrong.

The basic idea is that I want to host the service on a remote server so I want the service mapped to port localhost:7600 so that it can be invoked by posting data to [server_ip]:7600. The problem is most likely in the configuration file, since I am new to WCF and Rest I wasn't really sure what to type for the configuration so sorry if it's a total mess.

I removed several chunks of code and comments to make it a little easier to read. These functions should have no bearing on the service since they call only C# functions.

I looked at the post suggested, and rewrote the code, but unfortunately, it still is not functional. 我看了建议的帖子,并重写了代码,但不幸的是,它仍然没有功能。 Mabye I'm just using the wrong address, you would invoke this with http://localhost:7600 , right?

Thanks guys for all your help. 谢谢你们所有的帮助。 The problem was that you cannot use ServiceHost with a property that uses UriTemplate. So if I remove that, the service at least halfway works. I still am stuck on one part though. The service needs to be callable via HTTP Requests like you can produce with Fiddler. Any ideas on how I would do that?

NVM, that was a stupid question. NVM,这是一个愚蠢的问题。 Post data to http://localhost:7600/PCMiler_Connect_Imple and that returns the json data. Thanks again guys.

So this would be more helpful to someone else having the same problem, I have added the code as it is now, with a json invoke example. 所以这对其他有相同问题的人会更有帮助,我已经添加了现在的代码,并使用了json调用示例。

WCF Service Interface

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;

namespace PCMiler_Service
{
    [ServiceContract]
    public interface IPCMiler_Connect
    {
        [WebInvoke(Method = "POST",
                   ResponseFormat = WebMessageFormat.Json,
                   RequestFormat = WebMessageFormat.Json), //code corrected
        OperationContract]
        List<string> PCMiler_Connect_Imple(ZIP_List_Container container);
    }
}

WCF Service Implementation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace PCMiler_Service
{
    [DataContract]
    public class ZIP_List_Container
    {
        [DataMember]
        public string[] ZIP_List { get; set; }
        [DataMember]
        public string Optimized { get; set; }
        [DataMember]
        public string Calc_Type { get; set; }
        [DataMember]
        public string Cross_International_Borders { get; set; }
        [DataMember]
        public string Use_Kilometers { get; set; }
        [DataMember]
        public string Hazard_Level { get; set; }
        [DataMember]
        public string OK_To_Change_Destination { get; set; }
    }

    public class PCMiler_Connect : IPCMiler_Connect
    {
        public List<string> PCMiler_Connect_Imple(ZIP_List_Container container)
        {
            return container.ZIP_List.ToList<string>();
        }
    }
}

XML Config File

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="PCMiler_Service.PCMiler_ConnectBehavior">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="jsonBehavior"  >
              <enableWebScript />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="PCMiler_Service.PCMiler_ConnectBehavior"
                name="PCMiler_Service.PCMiler_Connect">
              <endpoint address="" behaviorConfiguration="jsonBehavior" binding="webHttpBinding"
                  contract="PCMiler_Service.IPCMiler_Connect" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:7600/" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

Service Wrapper

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.ServiceModel;
using System.Text;
using System.Threading;

namespace PCMiler_Service
{
    public partial class PCMiler_Service : ServiceBase
    {
        ServiceHost host;
        Thread thread;

        public PCMiler_Service()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            host = new ServiceHost(typeof(PCMiler_Connect));
            host.Open();
        }

        protected override void OnStop()
        {
            if (host != null)
            {
                host.Close();
                host = null;
            }
        }
    }
}

JSON POST Example with HTTP

POST /PCMiler_Connect_Imple HTTP/1.1
HOST: localhost:7600
Content-Type: application/json
Content-Length: 84

{
     "container": {
                    "ZIP_List":["29340","29614"]
      }
}

Probably a daft question but ... Where's your interface?

If you read this ...

http://weblogs.asp.net/ralfw/archive/2007/04/14/a-truely-simple-example-to-get-started-with-wcf.aspx

... it (like every other article on WCF) implies that an interface declaration is definately required in order to expose the service as it also defines the datacontract that is used when a client consumes the service.

This however does not look like your typical WCF service, more like a normal good old fashioned windows service which is not a WCF service (as you should see from that link), however it appears that you have ried to use some WCF component parts (like the contract attributes).

I think your problem is your missing interface declaration, I gather that WCF requires this.

我认为您需要使用WebServiceHost而不是ServiceHost。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM