环境:.NET Framework 3.5
服务: IIS EXpress托管 WCF服务程序
配置:Web.config
<!--<connectionStrings>
<add name="DbConnection"
connectionString="server=数据库地址;database=数据库名字;uid=用户名;pwd=密码;"/>
</connectionStrings>-->
<system.serviceModel> <services> <service name="TestWcfService.Service1" behaviorConfiguration="TestWcfService.Service1Behavior"> <!-- Service Endpoints --> <!--<endpoint address="" binding="basicHttpBinding" contract="TestWcfService.IService1" bindingConfiguration="LargeBuffer">--> <endpoint address="http://IP地址:端口号/PhoneApp/Service1.svc" binding="basicHttpBinding" contract="TestWcfService.IService1" bindingConfiguration="LargeBuffer"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="TestWcfService.Service1Behavior"> <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false --> <serviceMetadata httpGetEnabled="true"/> <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --> <serviceDebug includeExceptionDetailInFaults="false"/> <dataContractSerializer maxItemsInObjectGraph="2147483647" /> </behavior> </serviceBehaviors> </behaviors> <bindings> <basicHttpBinding> <binding name="LargeBuffer" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"> <readerQuotas maxStringContentLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxArrayLength="2147483647" /> <security mode="None" /> </binding> </basicHttpBinding> </bindings> </system.serviceModel>
Web.Debug.config and Web.Release.config
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <connectionStrings> <add name="DbConnection" connectionString="server=数据库地址;database=数据库名字;uid=用户名;pwd=密码;"/> </connectionStrings> <system.web> </system.web> </configuration>
说明:
本机调试程序的时候 打开Web.config 文件 绿色的标注的connectionStrings 和 endpoint address 节点 注销 红色的 endpoint address节点。调试成功
如果将WCF服务程序发布到网站的时候,关闭Web.config 文件 绿色的标注的connectionStrings 和 endpoint address 节点 打开 红色的 endpoint address节点。调试成功
为什么这么做:
客户端调用发布后的WCF服务接口的时候,有时候会出现 时而断时而连的情况。注明
<endpoint address="http://IP地址:端口号/PhoneApp/Service1.svc" binding="basicHttpBinding" contract="TestWcfService.IService1" bindingConfiguration="LargeBuffer">
这个节点说明是要访问指定的服务就不会出现 访问远程服务没有找到这个问题了 更新WCF服务的时候,要在客户端更新WCF服务,引用的就是新服务
配置好WCF服务程序在WCF服务程序里实现2步
1.上传图片到服务器 IService1.cs Service1.svc
namespace TestWcfService { [ServiceContract] public interface IService1 { [OperationContract] bool StationUpFile(UpFileContent Date); } public class UpFileContent { public byte[] ImageContent { get; set; } } }
public bool StationUpFile(UpFileContent Date) { bool IsSuccess = false; #region UpImage string DiskName = "e:"; string FileAddress = @"ProductImagesLarger"; string LocationAddress = DiskName + FileAddress; string filePath = string.Empty; using (Stream sourceStream = new MemoryStream(Date.ImageContent)) { if (!sourceStream.CanRead) { throw new Exception(""); } if (!Directory.Exists(LocationAddress)) { Directory.CreateDirectory(LocationAddress); } filePath = Path.Combine(LocationAddress, Date.StationImageName); if (!File.Exists(filePath)) { try { using (FileStream targetStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write)) { int count = 0; const int bufferlength = 4096; byte[] buffer = new byte[bufferlength]; while ((count = sourceStream.Read(buffer, 0, bufferlength)) > 0) { targetStream.Write(buffer, 0, count); } IsSuccess = true; } } catch { return IsSuccess; } } } LocationAddress = @"e:ProductImagesMiddle"; if (!Directory.Exists(LocationAddress)) Directory.CreateDirectory(LocationAddress); MakeSmallImg(filePath, Path.Combine(LocationAddress, Date.StationImageName), 940, 630); LocationAddress = @"e:ProductImagesSmall"; if (!Directory.Exists(LocationAddress)) Directory.CreateDirectory(LocationAddress); MakeSmallImg(filePath, Path.Combine(LocationAddress, Date.StationImageName), 320, 260); #endregion
//这里实现将图片名字写到数据库的某个字段里
}
此路径就是服务器的路径 (因为WCF服务程序部署到该服务器啦!写文件就写到这个服务器的路径下)
这是分成 大图 中图 小图的函数 可以略过
private void MakeSmallImg(string filePath, string fileSaveUrl, System.Double templateWidth, System.Double templateHeight) { using (Image myImage = System.Drawing.Image.FromFile(filePath)) { System.Double newWidth = myImage.Width, newHeight = myImage.Height; //宽大于模版的横图 if (myImage.Width > myImage.Height || myImage.Width == myImage.Height) { if (myImage.Width > templateWidth) { //宽按模版,高按比例缩放 newWidth = templateWidth; newHeight = myImage.Height * (newWidth / myImage.Width); } } //高大于模版的竖图 else { if (myImage.Height > templateHeight) { //高按模版,宽按比例缩放 newHeight = templateHeight; newWidth = myImage.Width * (newHeight / myImage.Height); } } System.Drawing.Size mySize = new System.Drawing.Size((int)newWidth, (int)newHeight); using (Image bitmap = new System.Drawing.Bitmap(mySize.Width, mySize.Height)) { using (Graphics graphics = Graphics.FromImage(bitmap)) { graphics.InterpolationMode = InterpolationMode.High; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.Clear(Color.Transparent); graphics.DrawImage(myImage, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), new System.Drawing.Rectangle(0, 0, myImage.Width, myImage.Height), System.Drawing.GraphicsUnit.Pixel); bitmap.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg); } } } }
2.将编译好的WCF服务程序 放到该网站。
接下来 要在wp客户端程序 更新引用服务
wp 客户端程序调用WCF服务
ServiceReference1.Service1Client Sc; public void Post(Action<bool> action) { //若用户选择了图片,则实例化 UploadPic 对象,用于上传图片 //注意:必须在UI线程实例化该对象! new Thread(() => { Sc = new ServiceReference1.Service1Client(); Sc.OpenAsync(); if (null != ImageSource) { MemoryStream fileStream = new MemoryStream(); using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isoStream = store.OpenFile(pic.FullPathName, FileMode.Open)) { isoStream.CopyTo(fileStream); } } //流转换为字节数组 byte[] tbuffer = fileStream.ToArray(); UpFileContent upfile = new UpFileContent(); upfile.StationImageName = pic.FileName; upfile.ImageContent = tbuffer; try { Sc.StationUpFileAsync(upfile); Sc.StationUpFileCompleted += (s1, e1) => { Deployment.Current.Dispatcher.BeginInvoke(() => { if (e1.Result) { fileStream.Close(); fileStream.Dispose(); } Deployment.Current.Dispatcher.BeginInvoke(() => { if (null != action) { Sc.CloseAsync(); ImageSource = null; action(true); } }); }); }; } catch (TimeoutException timeout) { Deployment.Current.Dispatcher.BeginInvoke(() => { if (null != action) { Sc.Abort(); ImageSource = null; MessageBox.Show(timeout.Message); action(false); } }); } catch (CommunicationException commException) { Deployment.Current.Dispatcher.BeginInvoke(() => { if (null != action) { Sc.Abort(); MessageBox.Show(commException.Message); ImageSource = null; action(false); } }); } } else { } }).Start(); }
THE END
WCF服务发布程序 到此告一段落!感谢!