2016-06-17 8 views
1

MVC5 web api2を使用してヘルプページを生成しようとするとエラーが発生します。プロジェクトを生成するときに生成されますが、これを生成しますが、リンクヘルプページを生成する際にエラーが発生する

System.StackOverflowExceptionが

未処理であったエラーは、このラインformatter.WriteToStreamAsync(type, value, ms, content, null).Wait();

に発生し、これが起こっているコードです。

[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The exception is recorded as InvalidSample.")] 
    public virtual object WriteSampleObjectUsingFormatter(MediaTypeFormatter formatter, object value, Type type, MediaTypeHeaderValue mediaType) 
    { 
     if (formatter == null) 
     { 
      throw new ArgumentNullException("formatter"); 
     } 
     if (mediaType == null) 
     { 
      throw new ArgumentNullException("mediaType"); 
     } 

     object sample = String.Empty; 
     MemoryStream ms = null; 
     HttpContent content = null; 
     try 
     { 
      if (formatter.CanWriteType(type)) 
      { 
       ms = new MemoryStream(); 
       content = new ObjectContent(type, value, formatter, mediaType); 
       formatter.WriteToStreamAsync(type, value, ms, content, null).Wait(); 
       ms.Position = 0; 
       StreamReader reader = new StreamReader(ms); 
       string serializedSampleString = reader.ReadToEnd(); 
       if (mediaType.MediaType.ToUpperInvariant().Contains("XML")) 
       { 
        serializedSampleString = TryFormatXml(serializedSampleString); 
       } 
       else if (mediaType.MediaType.ToUpperInvariant().Contains("JSON")) 
       { 
        serializedSampleString = TryFormatJson(serializedSampleString); 
       } 

       sample = new TextSample(serializedSampleString); 
      } 
      else 
      { 
       sample = new InvalidSample(String.Format(
        CultureInfo.CurrentCulture, 
        "Failed to generate the sample for media type '{0}'. Cannot use formatter '{1}' to write type '{2}'.", 
        mediaType, 
        formatter.GetType().Name, 
        type.Name)); 
      } 
     } 
     catch (Exception e) 
     { 
      sample = new InvalidSample(String.Format(
       CultureInfo.CurrentCulture, 
       "An exception has occurred while using the formatter '{0}' to generate sample for media type '{1}'. Exception message: {2}", 
       formatter.GetType().Name, 
       mediaType.MediaType, 
       UnwrapException(e).Message)); 
     } 
     finally 
     { 
      if (ms != null) 
      { 
       ms.Dispose(); 
      } 
      if (content != null) 
      { 
       content.Dispose(); 
      } 
     } 

     return sample; 
    } 

答えて

0

ウェブAPIプロジェクトでは、入力パラメータとしてオブジェクトを受け取ったメソッドとまったく同じ動作をします。たとえば、次のように

public IHttpActionResult Post(Person person) 

エラーが、私は関係のない別の名前空間に同じ名前(人)を持つクラスを追加した後に示し始め、その誤差は、完全修飾名前空間でパラメータを参照することにより、姿を消した:

public IHttpActionResult Post(MyProject.Models.Person person) 

これは誰かを助けることを願っています。

関連する問題