gpt4 book ai didi

c# - 从 Silverlight 使用 REST 服务时出现问题

转载 作者:太空宇宙 更新时间:2023-11-03 16:49:04 28 4
gpt4 key购买 nike

在我的 Web 项目中,我有一个包含 REST 服务的 TestStreamingService.svc 文件。

服务契约(Contract):

[ServiceContract(Namespace = "")]
public interface ITestStreamingService
{
[OperationContract]
[WebGet(UriTemplate = "Download?file={file}&size={size}")] //file irrelevant, size = returned size of the download
Stream Download(string file, long size);

[OperationContract]
[WebInvoke(UriTemplate= "Upload?file={file}&size={size}", Method = "POST")]
void Upload(string file, long size, Stream fileContent);

[OperationContract(AsyncPattern=true)]
[WebInvoke(UriTemplate = "BeginAsyncUpload?file={file}", Method = "POST")]
IAsyncResult BeginAsyncUpload(string file, Stream data, AsyncCallback callback, object asyncState);

void EndAsyncUpload(IAsyncResult ar);

}

服务实现(*.svc 文件)

使用系统;使用 System.IO;使用系统服务模型;使用 System.ServiceModel.Activation;使用 ICode.SHF.Tests;

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)][服务行为(InstanceContextMode=InstanceContextMode.Single)]公共(public)类 TestStreamingService : ITestStreamingService{

public Stream Download(string file, long size)
{
return new SHFTestStream(size);
}

public void Upload(string file, long size, Stream fileContent)
{
FileInfo f = new FileInfo(String.Format(@"C:\{0}", file));

using (FileStream fs = f.Create())
{
CopyStream(fileContent, fs);
fs.Flush();
fs.Close();
}
}

public IAsyncResult BeginAsyncUpload(string file, Stream data, AsyncCallback callback, object asyncState)
{
return new CompletedAsyncResult<Stream>(data, file);
}

public void EndAsyncUpload(IAsyncResult ar)
{
Stream data = ((CompletedAsyncResult<Stream>)ar).Data;
string file = ((CompletedAsyncResult<Stream>)ar).File;
StreamToFile(data, file);
}

private void StreamToFile(Stream data, string file)
{
string subDir = Guid.NewGuid().ToString("N");
string uploadDir = Path.Combine(Path.GetDirectoryName(typeof(TestStreamingService).Assembly.Location), subDir);
Directory.CreateDirectory(uploadDir);

byte[] buff = new byte[0x10000];

using (FileStream fs = new FileStream(Path.Combine(uploadDir, file), FileMode.Create))
{
int bytesRead = data.Read(buff, 0, buff.Length);
while (bytesRead > 0)
{
fs.Write(buff, 0, bytesRead);
bytesRead = data.Read(buff, 0, buff.Length);
}
}
}

公共(public)类 CompletedAsyncResult : IAsyncResult{ T数据;

string file;

public CompletedAsyncResult(T data, string file)
{ this.data = data; this.file = file; }

public T Data
{ get { return data; } }

public string File
{ get { return file; } }

#region IAsyncResult Members

public object AsyncState
{
get { return (object)data; }
}

public System.Threading.WaitHandle AsyncWaitHandle
{
get { throw new NotImplementedException(); }
}

public bool CompletedSynchronously
{
get { return true; }
}

public bool IsCompleted
{
get { return true; }
}

#endregion

我的 Web.Config

<?xml version="1.0"?>

<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->

<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>

<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="REST">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="ICode.SHF.SL.Tests.Web.TestStreamingService.customBinding0"/>
</webHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
/>
<services>
<service name="ICode.SHF.SL.Tests.Web.TestStreamingService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:40000/Streaming"/>
</baseAddresses>
</host>
<endpoint name="TestStreamingEndpoint" address="RESTService" binding="webHttpBinding" bindingConfiguration="ICode.SHF.SL.Tests.Web.TestStreamingService.customBinding0"
contract="ICode.SHF.SL.Tests.Web.ITestStreamingService" behaviorConfiguration="REST"/>

<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
</configuration>

(使用 WebClient.OpenWriteAsync(用于上传)和 OpenReadAsync(用于下载))

用于客户端的 uri 是:“http://localhost:40000/Streaming/Service/Download?file=xxx&size=65536”

当我在 IE 中使用以下 uri 时:“http://localhost:40000/TestStreamingService.svc/Download?file=xxx&size=65536”下载操作开始并且下载的文件与传递的大小匹配。

虽然我在 WebClient 中使用 IE uri 没有成功。

任何人都可以向我解释我做错了什么吗?看来我错过了一些基本的东西......

最佳答案

您的网站是否需要身份验证?至于 fiddler ,请尝试将您的网络客户端连接到:

http://localhost.:40000/Streaming/Service/Download?file=xxx&size=65536

(注意 localhost 之后的额外点)

关于c# - 从 Silverlight 使用 REST 服务时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4759854/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com