小编Dom*_*ton的帖子

使用WCF OperationContext通过Channel传递标头值

我有一个WCF服务,需要将应用程序ID参数传递给每个服务调用.目前我暴露的方法需要一个参数.我想尝试将此信息推送到Channel标头中.我的WCF使用Net.tcp托管.这是我的客户端代理代码:

public class CustomerClient : ClientBase<ICustomerBrowser>, ICustomerBrowser
{
  public Customer Get(string ApplicationID, string CustomerId)
  {
    try
    {
        using (OperationContextScope _scope = new OperationContextScope(this.InnerChannel))
        {
            MessageHeader _header = MessageHeader.CreateHeader("AppID", string.Empty, ApplicationId);
            OperationContext.Current.OutgoingMessageHeaders.Add(_header);
            return Channel.Get(ApplicationId, CustomerId);
            // return Channel.Get(CustomerId);
        }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

(注释掉的行是我想要继续使用的).

服务器代码:

var _context = WebOperationContext.Current;
var h = _context.IncomingRequest.Headers;
Run Code Online (Sandbox Code Playgroud)

在_context对象中有私有方法包含我的标题,但在_context.IncomingRequest.Headers中公开我得到这个:

public class CustomerClient : ClientBase<ICustomerBrowser>, ICustomerBrowser
{
  public Customer Get(string ApplicationID, string CustomerId)
  {
    try
    {
        using (OperationContextScope _scope = new OperationContextScope(this.InnerChannel))
        {
            MessageHeader _header = MessageHeader.CreateHeader("AppID", string.Empty, …
Run Code Online (Sandbox Code Playgroud)

c# wcf

7
推荐指数
1
解决办法
4759
查看次数

通过反射排除接口实现成员

我有以下接口和实现:

public interface INew
{
    string TestString { get; }
}

public class PurchaseOrder : INew
{
    public string OrderNo { get; set; }

    public string TestString
    {
        get { return "This is a test string"; }
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用以下代码反映对象的OrderNo一部分PurchaseOrder

var props = p.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.FlattenHierarchy);
foreach (var prop in props)
{
    Console.WriteLine(prop.Name);
}
Run Code Online (Sandbox Code Playgroud)

我的输出也返回了该TestString属性。我已经搜索了排除已实现的接口成员的方法,但只能找到包含它的项目。谁能告诉我如何排除这些项目?

c# reflection interface

5
推荐指数
1
解决办法
833
查看次数

ID 参数中带有反斜杠 (%2f) 的 WebApi 导致 404 未找到

我正在使用包含 / (%2f) 的参数调用 WebAPI 方法。如果我用没有这个字符的参数来调用它,它就可以正常工作。

按照此链接Escape characters in WebApi call中的建议,我尝试了以下操作,但没有任何区别。

<httpRuntime targetFramework="4.5.1" relaxedUrlToFileSystemMapping="true"/>
Run Code Online (Sandbox Code Playgroud)

我用来调用 WebAPI 方法的代码如下

var id = "AA/F1"; // This causes a problem of 404 not found
var workingId = "BB-F2"; // This works fine
using (var client = new HttpClient(new HttpClientHandler {AutomaticDecompression = DecompressionMethods.GZip}))
{
    var uri = new Uri(string.Format("http://[testServer]/[Controller]/Get/{0}", WebUtility.UrlEncode(id)));
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    var response = client.GetAsync(uri).GetAwaiter().GetResult();
    if (response.IsSuccessStatusCode)
        return JsonConvert.DeserializeObject<ReturnType>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());

    throw new Exception(response.ReasonPhrase);
}
Run Code Online (Sandbox Code Playgroud)

我只配置了一条路线 - 如下所示:

GlobalConfiguration.Configuration.Routes.MapHttpRoute("Default", "{controller}/{action}/{id}", new { id …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-web-api

5
推荐指数
1
解决办法
2926
查看次数

ODP.Net 新 OracleConnection“值不能为空参数名称:路径”

我有一个使用 ODP.Net 版本 12.2.1100 的项目。该项目在今天开业之前一直运行良好。以下代码行导致了错误:

using (var cn = new OracleConnection("User Name={userID}, etc, etc"))
Run Code Online (Sandbox Code Playgroud)

返回的错误是:

Value cannot be null. Parameter name: path

内部异常 at System.Security.Permissions.FileIOPermission.CheckIllegalCharacters(String[] str, Boolean onlyCheckExtras)

到目前为止我已经尝试解决该问题:

  1. 将连接字符串从 EZ Connect 更改为完整的 Oracle 字符串
  2. 卸载并重新安装 Oracle.ManagedDataAccess.dll(通过 NuGet)
  3. 确保所有配置文件都是最新的
  4. 创建了一个新的简单测试控制台项目(连接良好)

c# oracle

5
推荐指数
1
解决办法
2331
查看次数

Method.Invoke 因参数计数不匹配而失败

我正在尝试调用通用方法。该方法的定义如下:

public System.Collections.Generic.IList<T> Query<T>(string query, [string altUrl = ""])
where T : new()
Run Code Online (Sandbox Code Playgroud)

这是来自 github 上的 SalesforceSharp 库。我正在尝试在此调用上创建一个额外的服务层,并且正在努力调用它。请参阅下面的我的代码。

public List<T> Query<T>()
    {
        //IList<Salesforce.Account> _returnList = null;
        IList<T> _returnList = null;
        Type _t = typeof(T);

        SqlBuilder _sb = new SqlBuilder();
        _sb.Table = _t.Name.ToString();
        foreach (PropertyInfo p in _t.GetProperties()) _sb.Fields.Add(p.Name.ToString());

        MethodInfo method = _Client.GetType().GetMethod("Query");
        method = method.MakeGenericMethod(_t);
        try
        {
            object[] _prms = new object[1];
            _prms[0] = _sb.SQL;
            _returnList = (IList<T>)method.Invoke(_Client, new object[] { _prms });
            //_returnList = _Client.Query<Salesforce.Account>(_sb.SQL);
        }
        catch { } …
Run Code Online (Sandbox Code Playgroud)

c# generics reflection

4
推荐指数
1
解决办法
3503
查看次数

标签 统计

c# ×5

reflection ×2

asp.net-web-api ×1

generics ×1

interface ×1

oracle ×1

wcf ×1