WCF REST Question, binding, configuration

I am working on WCF rest interface using json. I wrapped the service in a Windows service to host this service, but now I am having problems with the fact that the service may be called. I'm not sure exactly what is wrong.

The main idea is that I want to host this service on a remote server, so I want the service to map the localhost: 7600 port so that it can be called by sending data to [server_ip]: 7600. The problem is most likely in the file configuration since I am new to WCF and Rest. I was not sure what to print for the configuration, so sorry if this is a complete mess.

I removed a few snippets of code and comments to make it a little easier to read. These functions should not affect maintenance, since they only call C # functions.

EDIT: I looked at the message suggested and rewrote the code, but unfortunately it still doesn't work. Mabye I'm using the wrong address, you would call this with http: // localhost: 7600 , right?

EDIT: Thanks guys for your help. The problem was that you cannot use ServiceHost with a property using UriTemplate. Therefore, if I remove this, the service will work at least halfway. However, I still adhere to one part. The service must be called through HTTP requests that you can create using Fiddler. Any ideas on how I would do this?

EDIT: NVM, that was a stupid question. Print data to http: // localhost: 7600 / PCMiler_Connect_Imple and return json data. Thanks again to the guys.

EDIT: So this would be more useful for someone else having the same problem, I added the code as it is now, with an example json call.

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);
    }
}

Deploying WCF Services

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 configuration 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 Wrap

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"]
      }
}
+3
source share
2 answers

Probably a dumb question, but ... Where is 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

... ( WCF) , , , , datacontract, , .

, , WCF, Windows, WCF ( ), , , WCF (, ).

, - , , WCF .

0

, WebServiceHost ServiceHost.

0

Source: https://habr.com/ru/post/1746115/


All Articles