[英]WCF: Windows service cannot find endpoint when hosted in Winforms application
[英]WCF Rest service referencing another application hosted WCF Service. Could not find default endpoint?
我的背景如下:我有一個WCF REST服務,它是消費者要與之通信的主要服務。
我有一個托管WCF服務的輔助應用程序(不是基於REST的),它公開了一些方法。 這個第二個是必須的,因為我需要基於REST的服務才能對32位非托管庫進行調用。
現在...我創建了我的自托管服務(為我包裝了對32位庫的幾個調用),如下所示:
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);
}
}
}
我運行此服務,然后轉到VS2013中的REST Service項目以用於Web。
我添加了對正在運行的該服務的引用。
然后,我構建我的REST服務並將.dll與自托管服務一起復制到我的服務器。
我運行自托管服務。
在我的Android應用程序中,我調用了REST服務。 REST服務從以下這一行開始:
writeMessage("Creating service reference");
ServiceReference1.BlahImageWarpServiceClient ImgWarpSvc = new ServiceReference1.BlahImageWarpServiceClient();
出現以下錯誤消息:
在ServiceModel客戶端配置部分中找不到引用合同'ServiceReference1.IBlahImageWarpService'的默認終結點元素。 這可能是因為找不到您的應用程序的配置文件,或者是因為在客戶端元素中找不到與該協定匹配的端點元素。
我猜這是另一個配置問題?
我是如此接近所有這些工作。 希望能朝正確的方向輕推或解決。
非常感謝。
當您調用不帶任何參數的ServiceReference1.BlahImageWarpServiceClient構造函數重載時,WCF將在客戶端配置文件的配置部分中查找引用該服務協定的條目。
您可以添加一個客戶端配置節,或者將Binding和EndpointAddress對象傳遞給構造函數。
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://localhost:8080/BlahImageWarp");
var client = new ServiceReference1.BlahImageWarpServiceClient(binding, address);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.