小编fla*_*ayn的帖子

app.config中的maxReceivedMessageSize和maxBufferSize

如何在运行应用程序之前将app.config文件中的maxReceivedMessageSize和maxBufferSize参数增加到2000000.

.net c# wcf winforms service-model

39
推荐指数
3
解决办法
10万
查看次数

内存流和大对象堆

我必须使用WCF通过不可靠的连接在计算机之间传输大文件.

因为我希望能够恢复该文件,并且我不希望受到WCF的文件大小限制,我将这些文件分块为1MB.这些"块"以流的形式传输.到目前为止哪个效果很好.

我的步骤是:

  1. 打开文件流
  2. 从文件中读取块到byte []并创建内存流
  3. 转移块
  4. 返回2.直到整个文件发送

我的问题在第2步.我假设当我从字节数组创建一个内存流时,它将最终在LOH上并最终导致outofmemory异常.我实际上无法创建此错误,也许我的假设是错误的.

现在,我不想在消息中发送byte [],因为WCF会告诉我数组大小太大.我可以更改允许的最大数组大小和/或我的块的大小,但我希望有另一种解决方案.

我的实际问题:

  • 我当前的解决方案是否会在LOH上创建对象并且会导致我的问题吗?
  • 有没有更好的方法来解决这个问题?

顺便说一句:在接收端,我简单地从到达流中读取较小的块并将它们直接写入文件,因此不涉及大字节数组.

编辑:

当前解决方案

for (int i = resumeChunk; i < chunks; i++)
{
 byte[] buffer = new byte[chunkSize];
 fileStream.Position = i * chunkSize;
 int actualLength = fileStream.Read(buffer, 0, (int)chunkSize);
 Array.Resize(ref buffer, actualLength);
 using (MemoryStream stream = new MemoryStream(buffer)) 
 {
  UploadFile(stream);
 }
}
Run Code Online (Sandbox Code Playgroud)

c# memory wcf out-of-memory large-object-heap

14
推荐指数
2
解决办法
8783
查看次数

匿名类实现接口

我在方法中有以下代码:

 var list = new[]
  {
   new { Name = "Red", IsSelected = true },
   new { Name = "Green", IsSelected = false },
   new { Name = "Blue", IsSelected = false },
  };
Run Code Online (Sandbox Code Playgroud)

我想调用一个函数,它需要一个元素列表,每个元素实现一个接口(ISelectable).我知道如何使用普通类完成此操作,但在这种情况下,我只是尝试填写一些演示数据.

是否可以创建实现接口的匿名类?

像这样:

new { Name = "Red", IsSelected = true } : ISelectable
Run Code Online (Sandbox Code Playgroud)

c# interface anonymous-class

14
推荐指数
2
解决办法
4890
查看次数

获取WCF的客户端IP地址,后期操作

我试图通过以下链接确定客户端的IP地址:http://www.danrigsby.com/blog/index.php/2008/05/21/get-the-clients-address-in-wcf/

在.Net 3.0中,没有可靠的方法来获取连接到WCF服务的客户端的地址.在.Net 3.5中引入了一个名为RemoteEndpointMessageProperty的新属性.此属性为您提供客户端连接进入服务的IP地址和端口.获取这些信息非常简单.只需通过RemoteEndpointMessageProperty.Name从当前OperationContext的IncomingMessageProperties中提取它,然后访问Address和Port属性.

> [ServiceContract] public interface IMyService {
>     [OperationContract]
>     string GetAddressAsString(); }
> 
> public class MyService : IMyService {
>     public string GetAddressAsString()
>     {
>         RemoteEndpointMessageProperty clientEndpoint =
>             OperationContext.Current.IncomingMessageProperties[
>             RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
> 
>         return String.Format(
>             "{0}:{1}",
>             clientEndpoint.Address, clientEndpoint.Port);
>     } } 
Run Code Online (Sandbox Code Playgroud)

注意事项:

  1. 此属性仅适用于http和tcp传输.在所有其他传输上,例如MSMQ和NamedPipes,此属性将不可用.
  2. 地址和端口由服务计算机的套接字或http.sys报告.因此,如果客户端通过VPN或其他修改地址的代理进入,则将表示该新地址而不是客户端的本地地址.这是可取和重要的,因为这是服务看到客户端的地址和端口,而不是客户端所看到的.这也意味着可能会有一些欺骗行为.客户端或服务器之间的某个客户端可能会欺骗地址.因此,除非添加一些其他自定义检查机制,否则不要使用地址或端口进行任何安全性决策.
  3. 如果您在服务上使用双工,那么不仅服务会为客户端填充此属性,而且客户端还将为该服务的每次调用填充此属性.

我有WebInvoke/Post和WebGet的operationContracts.当客户端请求是WebGet时,代码有效.但是当客户端请求是WebInvoke时,我将获得WCF主机IP.有解决方案吗 谢谢.

这是界面

[OperationContract]
[WebGet(UriTemplate = RestTemplate.hello_get)]
Stream hello_get();

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = RestTemplate.hello_post)]
Stream hello_post();

// Code for …
Run Code Online (Sandbox Code Playgroud)

wcf client ip-address webinvoke

12
推荐指数
1
解决办法
8311
查看次数

在Invoke-Command Cmdlet中处理ScriptBlock中的错误

我正在尝试使用powershell在远程计算机上安装服务.

到目前为止,我有以下内容:

Invoke-Command -ComputerName  $remoteComputerName -ScriptBlock {
         param($password=$password,$username=$username) 
         $secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
         $credentials = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
         New-Service -Name "XXX" -BinaryPathName "c:\XXX.exe" -DisplayName "XXX XXX XXX" -Description "XXXXXX." -Credential $credentials -ErrorVariable errortext 
         Write-Host("Error in: " + $errortext)
        } -ArgumentList $password,$username -ErrorVariable errortext 


Write-Host("Error out: " + $errortext)
Run Code Online (Sandbox Code Playgroud)

当执行New-Service时出错时,$ errortext ErrorVariable在ScriptBlock中正确设置,因为文本:"错误:显示错误.

Invoke-Command的ErrorVariable没有设置(我预期的).

我的问题是:

是否有可能将Invoke-Command的ErrorVariable设置为我在ScriptBlock中获得的错误?

我知道我也可以使用InstalUtil,WMI和SC来安装服务,但目前这不相关.

error-handling powershell invoke-command

10
推荐指数
3
解决办法
4万
查看次数

OpenXML嵌套表

我试图将表插入TableCell,但当我尝试在MS Word中打开它时,我得到一条消息:" a <p> is required before a </tc>".

当我在兼容模式下打开文件时,表格格式看起来很完美,但缺少表格行.将简单文本插入单元格工作正常,只有在我尝试插入整个表时才会出现问题.

将表格放在另一个表格之外的文档中也可以正常工作.

nested ms-word openxml tablecell

6
推荐指数
1
解决办法
1818
查看次数

WCF发现找到端点但主机是"localhost"

我试图使用http://msdn.microsoft.com/en-us/library/dd456783(v=VS.100).aspx作为起点在WCF中使用发现功能.它在我的机器上工作正常,但后来我想在另一台机器上运行该服务.该服务被正确发现,但找到的服务的主机名始终是"localhost",当然没什么用处.

服务端点:

var endpointAddress = new EndpointAddress(new UriBuilder { Scheme = Uri.UriSchemeNetTcp, Port = port}.Uri);
var endpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(IServiceInterface)), new NetTcpBinding (), endpointAddress);
Run Code Online (Sandbox Code Playgroud)

客户:

static EndpointAddress FindServiceAddress<T>()
{
  Stopwatch stopwatch = new Stopwatch();
  stopwatch.Start();
  DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
  // Find  endpoints            
  FindResponse findResponse = discoveryClient.Find(new FindCriteria(typeof(T)));
  Console.WriteLine(string.Format("Searched for {0} seconds. Found {1} Endpoint(s).",stopwatch.ElapsedMilliseconds / 1000,findResponse.Endpoints.Count));
  if (findResponse.Endpoints.Count > 0)
  {
     return findResponse.Endpoints[0].Address;
  }
  return null;
 }
Run Code Online (Sandbox Code Playgroud)

我应该简单地将主机设置为System.Environment.MachineName吗?

c# wcf service-discovery

4
推荐指数
2
解决办法
3236
查看次数

在C#中否定字符串

我正在编写一个简单的文件夹观察程序,我想忽略一个temp.temp文件,该文件在扫描时被复制到文件夹中,因此程序将检测放在文件夹中的任何内容并忽略temp.temp文件.目前我已经有程序检测IMG文件来解决问题.

if(e.FullPath.Contains("IMG"))            
{ 
    MessageBox.Show("You have a Collection Form: " + e.Name);
    Process.Start("explorer.exe", e.FullPath);
}
Run Code Online (Sandbox Code Playgroud)

c# string negate

0
推荐指数
1
解决办法
674
查看次数