【发布时间】:2014-06-01 23:10:45
【问题描述】:
我正在开发一个测试应用程序,我可以使用 Silver-light 应用程序将图像上传到 Web 服务器。
以下是我的 WCF SERVICE 的代码:
IImageService.cs 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel;
using System.Reflection;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IImageService" in both code and config file together.
[ServiceKnownType("GetKnownTypes", typeof(Helper))]
[ServiceContract]
public interface IImageService
{
[OperationContract]
int DoSum(int a, int b);
[OperationContract]
ResultInfo UploadPhoto(FileInfo fileInfo);
[OperationContract]
void UploadPhoto2(FileInfo fileInfo);
[OperationContract]
void UploadPhoto3(byte[] byteInfo);
}
// This class has the method named GetKnownTypes that returns a generic IEnumerable.
static class Helper
{
public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
{
System.Collections.Generic.List<System.Type> knownTypes =
new System.Collections.Generic.List<System.Type>();
// Add any types to include here.
knownTypes.Add(typeof(FileInfo));
knownTypes.Add(typeof(ResultInfo));
return knownTypes;
}
}
ImageService.cs 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ImageService" in code, svc and config file together.
public class ImageService : IImageService
{
public int DoSum(int a, int b)
{
return a + b;
}
public ResultInfo UploadPhoto(FileInfo fileInfo)
{
ResultInfo strResult = new ResultInfo();
try
{
if (fileInfo.Mode == "StaffImage")
{
if (fileInfo.ID.HasValue)
{
strResult.Result = "Staff image will be uploaded";
}
}
}
catch{}
return strResult;
}
public void UploadPhoto2(FileInfo fileInfo)
{
ResultInfo strResult = new ResultInfo();
try
{
if (fileInfo.Mode == "StaffImage")
{
if (fileInfo.ID.HasValue)
{
strResult.Result = "Staff image will be uploaded";
}
}
}
catch { }
}
public void UploadPhoto3(byte[] byteInfo)
{
}
}
[MessageContract]
public class FileInfo
{
[MessageBodyMember(Order = 1)]
public string FileName;
[MessageBodyMember(Order = 2)]
public string Mode;
[MessageBodyMember(Order = 3)]
public long? ID;
[MessageBodyMember(Order = 4)]
public Byte[] FileByte;
}
[MessageContract]
public class ResultInfo
{
[DataMember]
public string Result;
}
web.cofig 的代码
<configuration>
<appSettings>
<add key="PictureUploadDirectory" value="/UploadDocs"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0">
</compilation>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name="MyService">
<endpoint address="" binding="webHttpBinding" contract="IImageService" behaviorConfiguration="web"/>
<endpoint address="" binding="mexHttpBinding" contract="IImageService" behaviorConfiguration="MEX"/>
</service>
</services>
<bindings>
<webHttpBinding>
<binding maxReceivedMessageSize="2097152" maxBufferSize="2097152"
maxBufferPoolSize="2097152"
transferMode="Buffered"
bypassProxyOnLocal="false"
useDefaultWebProxy="true"
/>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="MEX">
<serviceMetadata />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
<behavior name="MEX">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
Silverlight 应用程序中的代码
ServiceReference1.ImageServiceClient objImgServiceClient = new ServiceReference1.ImageServiceClient();
objImgServiceClient.DoSumCompleted += objImgServiceClient_DoSumCompleted;
ServiceReference1.DoSumRequest req1 = new ServiceReference1.DoSumRequest();
req1.a = 2;
req1.b = 3;
objImgServiceClient.DoSumAsync(req1);
FileInfo _Info=new FileInfo();
_Info.FileName = "Test_File.jpg";
_Info.Mode = "StaffImage";
_Info.ID = 25;
_Info.FileByte = fileToSend;
objImgServiceClient.UploadPhotoCompleted += objImgServiceClient_UploadPhotoCompleted;
//objImgServiceClient.UploadPhotoAsync(_Info.FileName, _Info.Mode, _Info.ID, _Info.FileByte);
ServiceReference1.FileInfo objF = new ServiceReference1.FileInfo();
objF.FileByte = _Info.FileByte;
objF.Mode = _Info.Mode;
objF.ID = _Info.ID;
objF.FileName = _Info.FileName;
objImgServiceClient.UploadPhotoAsync(objF);
错误详情
当我访问 DoSum() 方法时,它会返回正确的结果。但是当我尝试使用 Stream/Byte 参数调用任何方法时,我会收到以下错误:
[System.ServiceModel.CommunicationException] = {System.ServiceModel.CommunicationException:远程服务器返回错误:NotFound。 ---> System.Net.WebException:远程服务器返回错误:NotFound。 ---> System.Net.WebException:远程服务器返回错误:NotFound。 在系统...
编辑:------------------
当我将 Byte[] 更改为 string 时,它可以完美运行,我遇到了 byte[] 数据类型的问题,因为我需要 Stream/byte[] 将图像上传到服务器。有什么想法吗?
[MessageContract]
public class FileInfo
{
[MessageBodyMember(Order = 1)]
public string FileName;
[MessageBodyMember(Order = 2)]
public string Mode;
[MessageBodyMember(Order = 3)]
public long? ID;
[MessageBodyMember(Order = 4)]
/*public Byte[] FileByte;*/
public string FileByte;
}
编辑:2---------------------
请检查以下屏幕截图,我无法将 maxReceivedMessageSize 设置为 WCF 服务,如果 Stream 内容很小,它会到达服务器,但不会很大。
谢谢
【问题讨论】:
-
您是否设置了端点地址?我在配置中没有看到它。
-
对不起,迟到的评论,你能解释一下我在这里缺少什么吗?我应该在 .config 文件中指定每个端点的地址吗?
-
是的,你有。请看我的回答,因为在评论中配置 sn-p 看起来很丑。
-
请在下面找到更新
-
请在我的 EDIT 3 中找到更新后的新错误答案。
标签: c# asp.net .net wcf silverlight