简体   繁体   English

WCF REST问题,绑定,配置

[英]WCF REST Question, Binding, Configuration

I am working on a WCF rest interface using json. 我正在使用json处理WCF休息接口。 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. 我已将服务包装在Windows服务中以托管服务,但我现在无法使服务可以调用。 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. 基本的想法是我想在远程服务器上托管服务,所以我希望服务映射到端口localhost:7600,以便可以通过将数据发布到[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. 这个问题最有可能出现在配置文件中,因为我是WCF的新手而且我不太确定要为配置键入什么,所以很抱歉,如果这是一团糟。

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. 这些功能应该与服务无关,因为它们只调用C#函数。

EDIT: 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? Mabye我只是使用了错误的地址,你可以用http:// localhost:7600来调用它,对吧?

EDIT: Thanks guys for all your help. 编辑:谢谢你们所有的帮助。 The problem was that you cannot use ServiceHost with a property that uses UriTemplate. 问题是您不能将ServiceHost与使用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. 该服务需要通过HTTP请求进行调用,就像您可以使用Fiddler生成一样。 Any ideas on how I would do that? 有关如何做到这一点的任何想法?

EDIT: NVM, that was a stupid question. 编辑: NVM,这是一个愚蠢的问题。 Post data to http://localhost:7600/PCMiler_Connect_Imple and that returns the json data. 将数据发布到http:// localhost:7600 / PCMiler_Connect_Imple并返回json数据。 Thanks again guys. 再次感谢你们。

EDIT: 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 WCF服务接口

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 WCF服务实现

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配置文件

<?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 使用HTTP的JSON POST示例

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 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. ...它(与WCF上的其他所有文章一样)意味着为了公开服务肯定需要接口声明,因为它还定义了客户端使用服务时使用的数据提取。

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). 然而,这看起来不像典型的WCF服务,更像是一个不是WCF服务的普通老式Windows服务(正如您应该从该链接中看到的那样),但是看起来您已经想要使用一些WCF组件(像合同属性)。

I think your problem is your missing interface declaration, I gather that WCF requires this. 我认为你的问题是你缺少的接口声明,我认为WCF需要这个。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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