简体   繁体   中英

WCF Rest service referencing another application hosted WCF Service. Could not find default endpoint?

My background is as follows: I have a WCF REST service which is the main service that consumers will communicate with.

I have a secondary application hosted WCF service (not REST based) which exposes a few methods. This secondary one is necessary because I need the REST based service to be able to make calls into a 32-bit unmanaged library.

Now...I created my self-hosted service (this wraps a couple of calls to the 32-bit library for me) like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Drawing;

namespace SelfHost
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri baseAddress = new Uri("http://localhost:8080/BlahImageWarp");
            Uri mexUri = new Uri("http://localhost:8080/BlahImageWarp/mex");

            // Create the ServiceHost.
            using (ServiceHost host = new ServiceHost(typeof(BlahImageWarpService), baseAddress))
            {
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetUrl = mexUri;
                smb.HttpGetEnabled = true;
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;

                host.Description.Behaviors.Add(smb);

                host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");

                BasicHttpBinding binding = new BasicHttpBinding();
                binding.MaxReceivedMessageSize = int.MaxValue;
                binding.Security.Mode = BasicHttpSecurityMode.None;

                host.AddServiceEndpoint(typeof(IBlahImageWarpService), binding, "");
                // Enable metadata publishing.

                var behavior = host.Description.Behaviors.Find<ServiceDebugBehavior>();
                behavior.IncludeExceptionDetailInFaults = true;

                host.Open();

                Console.WriteLine("The service is ready at {0}", baseAddress);
                Console.WriteLine("Press <Enter> to stop the service.");
                Console.ReadLine();

                // Close the ServiceHost.
                host.Close();
            }
        }
    }

    [ServiceContract]
    public interface IBlahImageWarpService
    {
        [OperationContract]
        string WarpImageJSON(string JSONString);

        [OperationContract]
        string BitmapToBase64(Bitmap bmp);
    }

    public class BlahImageWarpService : IBlahImageWarpService
    {
        /// <summary>
        /// warps the image
        /// </summary>
        /// <param name="JSONString">JSON of a BlahImgObject</param>
        /// <returns>returns base64Encoded warped image</returns>
        public string WarpImageJSON(string JSONString)
        {
            // this would be the call on the service
            BlahImageConverter conv = new BlahImageConverter(JSONString);
            var base64WarpedImage = conv.Warp();
            return base64WarpedImage;
        }

        public string BitmapToBase64(Bitmap bmp)
        {
            return BlahImageConverter.BitmapToBase64(bmp);
        }
    }
}

I run this service and then go to my REST Service project in VS2013 for web.

I add a reference to this service that's running.

I then build my REST service and copy the .dll to my server along with the self-hosted service.

I run the self-hosted service.

From my Android application I make the call to my REST service. The REST service craps out at this line:

writeMessage("Creating service reference");
ServiceReference1.BlahImageWarpServiceClient ImgWarpSvc = new ServiceReference1.BlahImageWarpServiceClient();

With the following error message:

Could not find default endpoint element that references contract 'ServiceReference1.IBlahImageWarpService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

I'm guessing it's another configuration issue?

I'm so close to having all of this working. Would appreciate a nudge in the right direction or a solution.

Thanks very much.

When you call the ServiceReference1.BlahImageWarpServiceClient constructor overload that doesn't take any parameters, WCF will look in the configuration section of the client configuration file for an entry that references that service contract.

You can either add a client config section, or pass Binding and EndpointAddress objects to the constructor.

BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://localhost:8080/BlahImageWarp");

var client = new ServiceReference1.BlahImageWarpServiceClient(binding, address);

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