我一直在尝试使用Microsoft提供的一些示例代码。我
错误CS0246无法找到类型或命名空间名称“System”(您是缺少使用指令还是程序集引用?)
警告是
警告无法解析程序集或Windows元数据文件“System.Runtime.dll”
它还显示了所有的系统导入,在它们下面有红线。听起来好像是不认识系统参考。
这是代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Networking.Sockets;
namespace P2PHelper
{
public class P2PSessionHost : P2PSession, IDisposable
{
private Dictionary<Guid, P2PClient> ClientMap { get; set; }
private StreamSocketListener SessionListener { get; set; }
private Timer Timer { get; set; }
public P2PSessionHost(P2PSessionConfigurationData config) : base(config)
{
this.SessionListener = new StreamSocketListener();
this.ClientMap = new Dictionary<Guid, P2PClient>();
}
public void Dispose()
{
this.SessionListener.Dispose();
this.SessionListener = null;
}
public async Task<bool> CreateP2PSession(SessionType type)
{
if (this.SessionListener == null) return false;
if (type != SessionType.LocalNetwork) throw new NotSupportedException(
"SessionType.LocalNetwork is the only SessionType supported.");
this.SessionHost = true;
this.SessionListener.ConnectionReceived += async (s, e) => await OnConnectionReceived(e.Socket);
await this.SessionListener.BindEndpointAsync(null, Settings.tcpPort);
this.InitializeNetworkInfo();
return await this.InitializeMulticast(null);
}
public bool RemoveClient(Guid clientID)
{
return this.ClientMap.Remove(clientID);
}
private bool AcceptingConnections { get; set; }
public void StartAcceptingConnections()
{
AcceptingConnections = true;
this.Timer = new Timer(async state => await SendMulticastMessage(""), null, 0, 500);
}
public void StopAcceptingConnections()
{
AcceptingConnections = false;
this.Timer.Dispose();
}
private async Task OnConnectionReceived(StreamSocket socket)
{
byte[] message = await RetrieveMessage(socket);
var newClient = new P2PClient { clientTcpIP = socket.Information.RemoteAddress.ToString() };
if (AcceptingConnections)
{
if (GetGuid(newClient).ToString() == (new Guid()).ToString())
{
Guid newGuid = Guid.NewGuid();
this.ClientMap.Add(newGuid, newClient);
this.OnConnectionComplete(newGuid);
}
}
this.OnMessageReceived(message, GetGuid(newClient));
}
private Guid GetGuid(P2PClient client)
{
return this.ClientMap.FirstOrDefault(
kvp => kvp.Value.clientTcpIP == client.clientTcpIP).Key;
}
protected async Task SendMulticastMessage(string output)
{
using (var multicastOutput = await this.MulticastSocket.GetOutputStreamAsync(
new Windows.Networking.HostName(Settings.multicastIP),
this.MulticastSocket.Information.LocalPort))
{
await multicastOutput.WriteAsync(Encoding.UTF8.GetBytes(output).AsBuffer());
}
}
public async Task<bool> SendMessage(Guid clientID, object message, Type type = null)
{
P2PClient client;
if (this.ClientMap.TryGetValue(clientID, out client))
{
return await base.SendMessage(message, client.clientTcpIP, Settings.tcpPort, type ?? typeof(object));
}
return false;
}
public async Task<bool> SendMessageToAll(object message, Type type = null)
{
var messageTasks = this.ClientMap.Keys.Select(guid => this.SendMessage(guid, message, type));
// When all the tasks complete, return true if they all succeeded.
return (await Task.WhenAll(messageTasks)).All(value => { return value; });
}
}
}发布于 2015-08-16 17:11:03
不识别.NET引用和名称空间(如System.Generics、System等),这通常是由于在客户端配置文件中编译或引用使用.NET的不同版本编译的DLL造成的。转到项目设置并验证正在编译的.NET的正确版本,没有打开客户端配置文件。
https://stackoverflow.com/questions/32036654
复制相似问题