在Azure Function V2中创建XmlSerializer时出现异常

时间:2018-03-07 19:04:15

标签: c# azure xml-serialization azure-functions

我有一个Azure Function V2,我正在尝试将对象序列化为XML。

我的对象在run.csx文件中声明。 (参见下面的代码。)

function.json文件

public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ViewHolder> {

    private final LayoutInflater layoutInflater;
    private final Context context;
    private ArrayList<Event> events;

    public ExampleAdapter(Context context, ArrayList<Event> events) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        this.events = events;
    }

    @Override
    public ExampleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(layoutInflater.inflate(R.layout.activity_row, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //holder.textView.setText(titles[position]);

        holder.title.setText(events.get(position).getTitle());
        holder.time.setText(events.get(position).getTime());
        holder.places.setText(events.get(position).getPlacesLeft());

    }

    public void add(ArrayList<Event> events){
        this.events = events;
        this.notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return events == null ? 0 : events.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        TextView time;
        TextView places;

        ViewHolder(View view) {
            super(view);

            title = (TextView) view.findViewById(R.id.activity_title);
            time = (TextView) view.findViewById(R.id.activity_time);
            places = (TextView) view.findViewById(R.id.activity_places);



            //textView = (TextView) view.findViewById(R.id.text_view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("ViewHolder", "onClick--> position = " + getPosition());
                }
            });
        }
    }
}

run.csx文件

{"frameworks": 
 {  
  "net461":
  { 
   "dependencies":
   {                  
     "System.Xml.XmlSerializer":"4.3.0"
   }
  }
 },
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    }
  ]
}

当我调用从我的FunctionResult对象类型创建XML Serializer时,它会抛出以下错误。

  

执行函数时出现异常:Functions.HttpTriggerCSharp1。   System.Private.CoreLib:目标抛出了异常   调用。 System.Private.CoreLib:路径不是合法形式。   参数名称:path。

当我记录'typeof(FunctionResult)'的结果时,我得到以下内容:

记录以下结果的代码:

#r "System.Xml.XmlSerializer"

using System.Net;
using System.Configuration;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;

public static IActionResult Run(HttpRequest req, TraceWriter log)
{
    var fileName = "test.dat";
    FunctionResult resultSet = new FunctionResult(fileName);

    string xmlString = "";

    log.Info("typeof:" + typeof(FunctionResult));
    //This next line throws the error.
    var xmlserializer = new XmlSerializer(typeof(FunctionResult));

    var stringWriter = new StringWriter();

    using (var writer =  XmlWriter.Create(stringWriter))
    {
        xmlserializer.Serialize(writer, resultSet.Rows);
        xmlString = stringWriter.ToString();
    }


}

//My Object has more attributes than this but this is a simplified version.
//I still get exceptions with this simple object.
public class FunctionResult
{
    public FunctionResult(string fileName)
    {                   
        FileName = fileName;
    }

    public string FileName { get; set; }        
}
  

2018-03-07T18:56:49.213 typeof:提交#0 + FunctionResult

1 个答案:

答案 0 :(得分:0)

采取Mikhail的回复并在此提及它,我认为您遇到了.NET Core的错误:https://github.com/dotnet/corefx/issues/26224

看起来这个修补程序将与.NET Core 2.1.0一起发布,所以一旦我们将这个版本的.NET Core添加到Azure Functions,这个问题就会得到解决。