WCF REST Question, Binding, Configuration

Posted by Ethan McGee on Stack Overflow See other posts from Stack Overflow or by Ethan McGee
Published on 2010-05-19T02:47:00Z Indexed on 2010/05/19 2:50 UTC
Read the original article Hit count: 390

Filed under:
|
|
|
|

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.

WCF Service Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;

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

    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class PCMiler_Webservice
    {    
        [WebInvoke(Method = "POST",                         
                   UriTemplate = "",           
                   ResponseFormat = WebMessageFormat.Json,  
                   RequestFormat = WebMessageFormat.Json),  
        OperationContract]
        public List<string> Calculate_Distance(ZIP_List_Container container)
        {

            return new List<string>(){ distance.ToString(), time.ToString() };
        }
    }
}

XML Config File

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
        <services>
            <service name="PCMiler_Connect.PCMiler_Webservice">
                <endpoint address="" behaviorConfiguration="jsonBehavior" binding="webHttpBinding"
                    bindingConfiguration="" contract="PCMiler_Connect.PCMiler_Webservice" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:7600/" />
                    </baseAddresses>
                </host>
            </service>
        </services>
        <behaviors>
            <endpointBehaviors>
              <behavior name="jsonBehavior">
                <enableWebScript/>
              </behavior>
            </endpointBehaviors>
        </behaviors>
    </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_WIN_Service
{
    public partial class Service1 : ServiceBase
    {
        ServiceHost host;

        public Service1()
        {
            InitializeComponent();
        }

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

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about wcf