1.項目結構背景介紹
注意我這裏是同一個解決方案中!!同一個解決方案中!!
新建wcf服務項目LookPictureService,新建控制檯項目用於寄宿wcf服務TestLookPictureService,新建控制檯項目TestLookPictureService1作爲客戶端測試wcf服務,如下:
項目名稱 | 功能解釋 |
---|---|
LookPictureService | wcf服務項目 |
TestLookPictureService | 控制檯項目,用於寄宿wcf服務 |
TestLookPictureService1 | 控制檯項目,作爲客戶端用於測試wcf服務 |
TestLookPictureService引用LookPictureService,如下:
TestLookPictureService使用代碼形式進行寄宿,如下:
using LookPictureService;
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace TestLookPictureService
{
class Program
{
static void Main(string[] args)
{
// Step 1: Create a URI to serve as the base address.
Uri baseAddress = new Uri("http://localhost:8734/Design_Time_Addresses/LookPictureService/CalculatorService/");
Uri baseAddress1 = new Uri("http://localhost:8734/Design_Time_Addresses/LookPictureService/LookPictureService/");
// Step 2: Create a ServiceHost instance.
ServiceHost selfHost = new CustomServiceHost(typeof(CalculatorService), baseAddress);
ServiceHost selfHost1 = new CustomServiceHost(typeof(LookPictureService.LookPictureService), baseAddress1);
try
{
//修改默認Binding
BasicHttpBinding binding = new BasicHttpBinding();
binding.MaxBufferSize = 2147483647;
binding.MaxReceivedMessageSize = 2147483647;
// Step 3: Add a service endpoint.
selfHost.AddServiceEndpoint(typeof(ICalculator), new BasicHttpBinding(), "CalculatorService");
selfHost1.AddServiceEndpoint(typeof(ILookPictureService), binding, "LookPictureService");//使用修改的的Binding
// Step 4: Enable metadata exchange.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
ServiceMetadataBehavior smb1 = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb1.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
selfHost1.Description.Behaviors.Add(smb1);
// Step 5: Start the service.
selfHost.Open();
selfHost1.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("The service1 is ready.");
// Close the ServiceHost to stop the service.
Console.WriteLine("Press <Enter> to terminate the service.");
Console.WriteLine("Press <Enter> to terminate the service1.");
Console.WriteLine();
Console.ReadLine();
selfHost.Close();
selfHost1.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
}
}
}
}
TestLookPictureService1添加對TestLookPictureService的服務引用(注意添加服務引用前需啓動TestLookPictureService控制檯程序),如下:
2.配置wcf服務於寄宿程序
在wcf服務項目中的App.config文件中,配置system.web節點,增加compliation debug=“true”,如下:
配置完wcf服務的App.config文件之後,重新生成解決方案,重新生成寄宿程序TestLookPictureService解決方案,更新TestLookPictureService1客戶端服務引用。
3.斷點F11單步調試
做完以上步驟,可以在TestLookPictureService1客戶端程序中下斷點,如下:
使用F11單步調試,這時就進入到wcf服務項目LookPictureService中來啦,如下:
注意,我的這三個項目都在同一個解決方案中哦!!!