WCF宿主于WPF

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Xml;
using FileClassLibrary;
using Host;

namespace WpfApplicationWCFServer
{
///<summary>
/// Interaction logic for WCFServer.xaml
///</summary>
public partial class WCFServer : Window
{
ServiceHost host;
void WCFInit()
{
host = new ServiceHost(typeof(Host.WCFService));

//WSHttpBinding WSHB = new WSHttpBinding();
WSHttpBinding WSHB = new WSHttpBinding(SecurityMode.None);
WSHB.Name = "WSHttpBinding_IWCFService";
//Uri u = new Uri("asdas");

#region 设置客户端可以发送最大文件大小,这里设置为无限制
WSHB.MaxBufferPoolSize = int.MaxValue;
WSHB.MaxReceivedMessageSize = int.MaxValue;
XmlDictionaryReaderQuotas XDRQ = new XmlDictionaryReaderQuotas();
XDRQ.MaxArrayLength = int.MaxValue;
XDRQ.MaxStringContentLength = int.MaxValue;
XDRQ.MaxBytesPerRead = int.MaxValue;
WSHB.ReaderQuotas = XDRQ;
#endregion

#region tcp
//NetTcpBinding netbinding = new NetTcpBinding();
//netbinding.MaxBufferPoolSize = int.MaxValue;
//netbinding.MaxBufferSize = int.MaxValue;
//netbinding.TransferMode = TransferMode.Streamed;//这里指定传输为流传输模式。
//XmlDictionaryReaderQuotas readerQuotas = new XmlDictionaryReaderQuotas();
//readerQuotas.MaxArrayLength = int.MaxValue;
//readerQuotas.MaxStringContentLength = int.MaxValue;
//readerQuotas.MaxBytesPerRead = int.MaxValue;
//netbinding.ReaderQuotas = readerQuotas;
//service.AddServiceEndpoint(typeof(interfaces), netbinding, "net.tcp://192.168.18.100:8000");
#endregion
host.AddServiceEndpoint(typeof(Host.IWCFService),WSHB,"http://" + IP地址textBox.Text + ":" + 端口TextBox.Text + "/WCFService/");
if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
{
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
behavior.HttpGetUrl = new Uri("http://" + IP地址textBox.Text + ":" + 端口TextBox.Text + "/WCFService/");
host.Description.Behaviors.Add(behavior);
}
ServiceDebugBehavior debugBehavior = host.Description.Behaviors.Find<ServiceDebugBehavior>();
if (debugBehavior == null)
{
debugBehavior = new ServiceDebugBehavior();
host.Description.Behaviors.Add(debugBehavior);
}
debugBehavior.IncludeExceptionDetailInFaults = false;




ServiceEndpoint SEp = host.Description.Endpoints[0];

SEp.Binding.Name = "WSHttpBinding_IWCFService";


}
public WCFServer()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
接收路径textBox.Text = System.Windows.Forms.Application.StartupPath;
IP地址textBox.Text = CommonFunction.GetServerIPStr();
}
private void 启动服务器_Click(object sender, RoutedEventArgs e)
{
WCFInit();
WCFService.strRemotePath = 接收路径textBox.Text;
host.Open();
label1.Content = "启动成功!";
//using (ServiceHost host = new ServiceHost(typeof(Host.WCFService)))
//{
// host.Open();
// label1.Content = "启动成功!";
//}
}

private void 关闭服务器_Click(object sender, RoutedEventArgs e)
{
host.Close();
label1.Content = "关闭成功!";

}
public void 选择文件()
{
System.Windows.Forms.OpenFileDialog od = new System.Windows.Forms.OpenFileDialog();
System.Windows.Interop.HwndSource source = PresentationSource.FromVisual(this) as System.Windows.Interop.HwndSource;
System.Windows.Forms.IWin32Window win = new OldWindow(source.Handle);
od.ShowDialog(win);
}
public string 选择文件夹()
{
System.Windows.Forms.FolderBrowserDialog fd = new System.Windows.Forms.FolderBrowserDialog();
fd.ShowNewFolderButton = false;
//fd.RootFolder = Environment.SpecialFolder.Programs;//指定对话框的起始根文件夹
fd.Description = "选择上传路径";//指定对话框显示的文字说明

System.Windows.Interop.HwndSource source = PresentationSource.FromVisual(this) as System.Windows.Interop.HwndSource;
System.Windows.Forms.IWin32Window win = new OldWindow(source.Handle);


System.Windows.Forms.DialogResult result = fd.ShowDialog(win);
if (result == System.Windows.Forms.DialogResult.OK)
{
return fd.SelectedPath;
}
return System.Windows.Forms.Application.StartupPath;
}

private void 选择接收路径_Click(object sender, RoutedEventArgs e)
{
接收路径textBox.Text = 选择文件夹();
Host.WCFService.strRemotePath = 接收路径textBox.Text;
}
}
}
public class OldWindow : System.Windows.Forms.IWin32Window
{
IntPtr _handle;
public OldWindow(IntPtr handle)
{
_handle = handle;
}

#region IWin32Window Members
IntPtr System.Windows.Forms.IWin32Window.Handle
{
get
{
return _handle;
}
}
#endregion
}

wcf服务器生成代理类后 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using ServiceReference;
using System.IO;
using FileClassLibrary;

namespace SendCall.WCF
{
public class WCFCallClass
{
public IWCFService IWS;
public WCFCallClass()
{
WSHttpBinding WSHB = new WSHttpBinding(SecurityMode.None);
WSHB.Name = "wsHttpBindingConfiguration_IWCFService";
//IWS = new WCFServiceClient(WSHB, new EndpointAddress(WCFCallClass.strSendPath));
WCFServiceClient WC = new WCFServiceClient(WSHB, new EndpointAddress("http://localhost:8731/WCFService/"));
WC.Endpoint.Binding.Name = "wsHttpBindingConfiguration_IWCFService";
IWS = WC;
}
public WCFCallClass(String SendFilePath)
{
WSHttpBinding WSHB = new WSHttpBinding(SecurityMode.None);
WSHB.Name = "wsHttpBindingConfiguration_IWCFService";
//IWS = new WCFServiceClient(WSHB, new EndpointAddress(WCFCallClass.strSendPath));
WCFServiceClient WC = new WCFServiceClient(WSHB, new EndpointAddress(SendFilePath));
WC.Endpoint.Binding.Name = "wsHttpBindingConfiguration_IWCFService";
IWS = WC;
}
public int SendFile(FileHeader FH, byte[] data)
{
return SendFileprivate(FH, data);
}
private int SendFileprivate(FileHeader FH, byte[] data)
{
try
{
IWS.FileReceive(FH, data);
return 1;
}
catch (Exception ex)
{
return 0;
}
}
public int HelloWCF()
{
try
{
IWS.HelloWCF("是否连接");
return 1;
}
catch (Exception ex)
{
return 0;
}
}
}

}

服务器代码:接口我就不写了

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using FileClassLibrary;
using System.IO;

namespace Host
{
// 注意: 如果更改此处的类名 "HelloWCFService",也必须更新 App.config 中对 "HelloWCFService" 的引用。
public class WCFService : IWCFService
{
public static String strRemotePath = "C:\\";//文件默认写在c盘
///<summary>
/// 链接成功,测试链接是否成功
///</summary>
///<param name="message"></param>
///<returns></returns>
public string HelloWCF(string message)
{
//return string.Format("你在{0}收到信息:{1}", DateTime.Now, message);
return "1";
}
///<summary>
/// 最大传输数据服务器可以控制
///</summary>
///<param name="FH"></param>
///<param name="data"></param>
///<returns></returns>
public string FileReceive(FileHeader FH, byte[] data)
{

string FolderPath = WCFService.strRemotePath + "\\" + FH.FolderName;
if (Directory.Exists(FolderPath) == false)//如果不存在就创建file文件夹
{
Directory.CreateDirectory(FolderPath);
}
string FileNames = FolderPath + "\\" + FH.SendFileName;
FileStream fileStream;
if (File.Exists(FileNames))
{
fileStream = new FileStream(FileNames, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);//附加
}
else
{
fileStream = new FileStream(FileNames, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite);//新增
}
BinaryWriter writer = new BinaryWriter(fileStream);

if (data.Length > 0)
{
//将接收到的数据包写入到文件流对象
writer.Write(data, 0, data.Length);
}

writer.Close();
return "1";//接收成功
}
}
}
原文地址:https://www.cnblogs.com/cuihongyu3503319/p/2226969.html