Ambiguous references when adding a WebService to Silverlight (3.0)?
-
13-06-2021 - |
문제
When I add my WebService I started getting all these errors:
'Stream' is an ambiguous reference between 'System.IO.Stream' and 'MultiSelectFileUploader.ServiceReference.Stream'
This wasn't happening when all I had in there was the GetFiles(). And I can go ahead and change my Streams to System.IO.Stream, but then I get the error that it isn't a ServiceReference.Stream when I try to pass to the UploadFiles(). Thanks for the assitance.
Here's my WebService...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
namespace MultiSelectFileUploader.Web
{
/// <summary>
/// Summary description for FileService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class FileService : System.Web.Services.WebService
{
[WebMethod]
public List<string> GetFiles()
{
List<string> l = new List<string>();
foreach (FileInfo fi in (new DirectoryInfo(Server.MapPath("~/Temp/")).GetFiles()))
l.Add(fi.Name);
return l;
}
[WebMethod]
public void UploadFiles(string filename, Stream file)
{
string filepath = AppDomain.CurrentDomain.BaseDirectory + @"Temp\";
if (!Directory.Exists(filepath)) { Directory.CreateDirectory(filepath); }
WriteFile(file, File.Create(Context.Server.MapPath("~/Temp/" + filename)));
}
private void WriteFile(Stream stream, FileStream fs)
{
int bytesRead;
byte[] buffer = new byte[4096];
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
fs.Write(buffer, 0, bytesRead);
}
}
}
해결책
Streaming is not supported in ASMX services. You have to use WCF.
다른 팁
It sounds like you have two classes called Stream, in two namespaces, in separate assemblies (DLLs).
If you need to use both classes called Stream in the same C# class, you would be best to fully qualify the class names where you use them (there are other ways, but imho this is the cleanest).
You're getting isn't a ServiceReference.Stream
presumably because you changed the declaration somewhere to System.IO.Stream
where in fact a MultiSelectFileUploader.ServiceReference.Stream
is expected.
Since you have two different versions of Stream
(one in System.IO
and one in MultiSelectFileUploader.ServiceReference
), you'll need to have a way of differentiating between them.
In addition, the error you're getting now is because you are not using the MultiSelectFileUploader.ServiceReference
namespace anywhere, so your code doesn't know of a MultiSelectFileUploader.ServiceReference.Stream
class.
I would recommend using aliases for System.IO
and MultiSelectFileUploader.ServiceReference
, like so:
using IO = System.IO;
using Uploader = MultiSelectFileUploader.ServiceReference;
and then using the aliases to specify which stream you are using, like so (assuming this is where you're using the MultiSelectFileUploader.ServiceReference.Stream class):
private void WriteFile(Uploader.Stream stream, FileStream fs)
{
...
}
The reason it didn't happen before was that GetFiles had no explicit reference to a Stream class.